0

I have a very old Objective-C app that I am trying to convert to Swift. I am not well versed in Objective-C and even less so in pointers.
I have this code here that I can not figure out what to do with:

NSData *ucTimeData=[self getUcTime];
Byte *ucTimeByte = (Byte *)[ucTimeData bytes];

The NSData looks like this when printing the description:

Printing description of data:
<9ea49c1d>

What would be the Swift equivalent of the Byte conversion? ucTimeData.bytes returns an unsafe pointer so something like:

let ucTimeByte: UInt8 = UInt8(ucTimeData.bytes)

which I guess would be the literal translation but it does not work.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
Notsileous
  • 548
  • 3
  • 9
  • 3
    Without knowing how you're going to _use_ those bytes, it's hard to give a good recommendation here. – jnpdx Jun 14 '23 at 17:35
  • 1
    General rule is it's a bad idea to convert from ObjC to Swift line by line. It's not always beneficial to even convert a single function. In many cases you need to look at a bigger picture: which inputs you need to handle, which outputs you expect, and are there Swift libraries that already handle what previously was done by hand. This especially applies to pointers - there's almost no reasons to use them in Swift. So please provide more context on what you are converting, and what are the inputs, and expected outputs – timbre timbre Jun 14 '23 at 19:24
  • Seems that that your data contains an array of bytes. utcTimeBute is a pointer to an array of uint8 aka bytes. Look at [this](https://stackoverflow.com/questions/31821709/nsdata-to-uint8-in-swift) – Ptit Xav Jun 15 '23 at 20:44

0 Answers0