为什么我会在
处发送一个无法识别的选择器doesExist = [myStr isEqualToString:@"Hello"];
- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"%s", __PRETTY_FUNCTION__);
NSLog(@"Found %d peripheral as a result of scanning", myListOfPeripherals.count);
NSString *strMACId = [advertisementData valueForKey:@"kCBAdvDataManufacturerData"];
NSLog(@"%@", strMACId);
[timerConnectionTimeout invalidate];
PeripheralCell * objPeripheralCell;
NSLog(@"Found Peripheral with Name: %@ RSSI data:%@ AdvData: %@", peripheral.name, peripheral.RSSI, advertisementData);
// Check if the Peripheral already exists in your collection - if no add it.
if ([self peripheralExistsWithMacId:strMACId] == false)
{
// New peripheral - Add it to the list of myPeripherals
objPeripheralCell=[[PeripheralCell alloc] init];
[myListOfPeripherals addObject: objPeripheralCell];
objPeripheralCell.peripheral=peripheral;
objPeripheralCell.rssi=RSSI;
objPeripheralCell.peripheralMacId = strMACId;
}
}
-(BOOL) peripheralExistsWithMacId:(NSString *)strMacId
{
BOOL doesExist = false;
for (int i=0; i<myListOfPeripherals.count; i++)
{
PeripheralCell *objPeripheralCell = myListOfPeripherals[i];
NSString *myStr = objPeripheralCell.peripheralMacId;
NSLog(@"Comparing %@ with %@", myStr, strMacId);
**doesExist = [myStr isEqualToString:@"Hello"];**
if (doesExist)
{
break;
}
}
return doesExist;
}
错误 - &gt;&gt; -[NSConcreteData isEqualToString:]: unrecognized selector sent to instance 0x7970e90
更新: NSLog(@“%@”,strMACId)产生&lt; 00ff6e62 61bacad8&gt;我怎么知道这不是NSString对象
答案 0 :(得分:4)
您的代码行:
[advertisementData valueForKey:@"kCBAdvDataManufacturerData"];
返回NSConcreteData并将其分配给NSString类型的指针。您在Xcode中没有收到任何代码错误的原因是它认为您在分配特定类型的指针时知道自己在做什么。然后它看到你在那个它认为是NSString的指针上调用isEqualToString
,从而将该调用视为完全逻辑和功能。如果你添加了一行代码(或者甚至是NSLog),检查对象是否实际上是一个NSString,你会发现它不是。
if([strMACId isKindOfClass:[NSString class]])
{
//This will return false for your case, and never get in here
}
您需要做的是从NSDictionary中读取值,然后将其转换为NSString对象。你可以使用类似的东西:
NSString* newStr = [[NSString alloc] initWithData:theData
encoding:NSUTF8StringEncoding]
但是你必须要小心编码。有一些循环编码样式的方法,直到你得到一个可以无错误地解码你的NSData,但我不确定你的数据是否可以正确解码为NSString。另一种选择是将PeripheralCell.peripheralMacId
的类型更改为NSData并比较数据以获得所需的数据。