1

这是我的代码:

- (void)peripheralManager:(CBPeripheralManager *)peripheralManager central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic
{
    [self.centralManager retrievePeripherals:@[central.UUID]];
}

我收到一个错误:

Collection element of type 'CFUUIDRef' (aka 'const struct __CFUUID *') is not an Objective-C object

我该怎么办?

4

3 回答 3

3

问题:

编译器只知道类型,而不知道运行时行为。它不知道 - 很可能 -CFUUIDRef可以像任何普通的 Objective-C 对象一样使用(尽管它没有正式的免费桥接 Foundation 类对应物)。它只看到它const struct __CFUUID不是一个 Objective-C 类,它就退出了。

解决方案:

I.我认为这会起作用- 刚刚尝试过它确实有效,CFUUID甚至在使用打印时有一个很好的描述NSLog().但是,它没有记录。只需将其转换为id,如下所示:

@[(__bridge id)central.UUID]

二、是的,您可以将其转换为字符串,但这也不会使编译器错误消失 - 您确实需要那种类型转换,因为编译器对不兼容的类型会产生怪癖:

CFStringRef uuidString = CFUUIDCreateString(kCFAllocatorDefault,@[central.UUID]);
NSString *uuidNSString = (__bridge NSString *)uuidString;

现在这可以保证工作。

于 2013-07-18T16:07:05.850 回答
1

我的建议是将其转换为 NSString 并将其添加到数组中。

+ (NSString *)convertUUID:(CFUUIDRef)theUUID
{
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return (__bridge_transfer NSString *)string;
}
于 2013-07-18T16:02:49.843 回答
1

尝试将其转换为 Objective-C 对象:

CFStringRef uuidString = CFUUIDCreateString(kCFAllocatorDefault, yourUUID);
NSString *uuidNSString = (__bridge NSString *)uuidString;

如果你需要它回来:

CFUUIDRef uuid = CFUUIDCreateFromString(kCFAllocatorDefault, uuidString);
于 2013-07-18T16:03:02.287 回答