最近,我迅速找到了源代码,并试图将其发布到Objective-C。我无法理解的一件事是:
var theData:UInt8! theData = 3; NSData(bytes: [theData] as [UInt8], length: 1)
有人可以帮我获得与Obj-C相当的产品吗?
为了给您提供一些背景信息,我需要将UInt8作为UInt8发送到CoreBluetooth外设(CBPeripheral)。浮点数或整数将不起作用,因为数据类型太大。
如果您将Swift代码编写为稍微简单一点
var theData : UInt8 = 3 let data = NSData(bytes: &theData, length: 1)
那么将其转换为Objective-C相对简单:
uint8_t theData = 3; NSData *data = [NSData dataWithBytes:&theData length:1];
对于多个字节,您将使用数组
var theData : [UInt8] = [ 3, 4, 5 ] let data = NSData(bytes: &theData, length: theData.count)
转换为Objective-C为
uint8_t theData[] = { 3, 4, 5 }; NSData *data = [NSData dataWithBytes:&theData length:sizeof(theData)];