0

I'm writing to a CoreBluetooth device using

- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;

in objective C

I would like to move this code over to Swift. Therefore I'm writing using

 open func writeValue(_ data: Data, for characteristic: CBCharacteristic, type: CBCharacteristicWriteType)

All of the parameter values being sent are the same except data when sent as NSData will be represented as such <000a0302>. In Swift the data is represented as

▿ 4 bytes - count : 4 ▿ pointer : 0x00000002810ce5b0 - pointerValue : 10755040688 ▿ bytes : 4 elements - 0 : 0 - 1 : 10 - 2 : 3

  • 3 : 2

However I am not getting the same result back from the bluetooth peripheral - what is the difference between the two examples?

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • 1
    Possibly helpful: [How to convert Data to hex string in swift](https://stackoverflow.com/q/39075043/1187415) – Martin R Jan 28 '19 at 12:47
  • 1
    See also: [Why Data and NSData print differently](https://stackoverflow.com/q/44830747/1187415) – Martin R Jan 28 '19 at 12:50

2 Answers2

4

Practically there is no difference. Data is implicitly bridged to NSData and vice versa if necessary.

The difference is how NSData and Data are displayed to the console.

  • NSData is represented by the hex bytes in angle brackets (<000a0302>).
  • Data is represented by the number of bytes (4 bytes). If you want the NSData representation add as NSData in the print expression
vadian
  • 274,689
  • 30
  • 353
  • 361
  • I'll had that to debug, I use `print(\(data as NSData))` to force the `description` of `NSData` which might be more useful (except, it's a large data). – Larme Jan 28 '19 at 12:48
  • po data as NSData shows <000a0302> - the issue may be that the peripheral is expecting hex – Samuel Harvey Jan 28 '19 at 13:28
0

They are exactly the same - I had some nasty wrapper code causing a bug which is now squashed!