获取 CFMutableDictionaryRef 值
我想从 CFMutableDictionaryRef 访问一些值,然后对它们进行一些数学运算。使用下面的代码,我成功地打印了电池的一些属性。我还可以打印出单个值(尽管这样做时我收到警告:无效的接收器类型:CFMutableDictionaryRef)。
然后我尝试将字符串值转换为 NSNumber,然后程序出现错误。它给了我类似无法识别的选择器发送到实例 0x5d65f70 有帮助吗?
CFMutableDictionaryRef matching , properties = NULL;
io_registry_entry_t entry = 0;
matching = IOServiceMatching( "IOPMPowerSource" );
//matching = IOServiceNameMatching( "AppleSmartBattery" );
entry = IOServiceGetMatchingService( kIOMasterPortDefault , matching );
IORegistryEntryCreateCFProperties( entry , &properties , NULL , 0 );
NSLog( @"%@" , properties );
NSString * voltage = [properties objectForKey:@"Voltage"];
NSString * currentCapacity = [properties objectForKey:@"CurrentCapacity"];
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:voltage];
[f release];
CFRelease( properties );
IOObjectRelease( entry );
I would like to access some values from a CFMutableDictionaryRef and then do some math on them. Using the code below I have managed to print some properties of my battery. I am also able to print out single values (although in doing so I get a warning:invalid receiver type:CFMutableDictionaryRef).
Then I try to convert the string values to NSNumber and then the program bugs. It gives me something like unrecognized selector sent to instance 0x5d65f70 Any help?
CFMutableDictionaryRef matching , properties = NULL;
io_registry_entry_t entry = 0;
matching = IOServiceMatching( "IOPMPowerSource" );
//matching = IOServiceNameMatching( "AppleSmartBattery" );
entry = IOServiceGetMatchingService( kIOMasterPortDefault , matching );
IORegistryEntryCreateCFProperties( entry , &properties , NULL , 0 );
NSLog( @"%@" , properties );
NSString * voltage = [properties objectForKey:@"Voltage"];
NSString * currentCapacity = [properties objectForKey:@"CurrentCapacity"];
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:voltage];
[f release];
CFRelease( properties );
IOObjectRelease( entry );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是因为编译器不知道 CFMutableDictionaries 也是 NSMutableDictionaries。您必须使用显式强制转换来告诉编译器可以将消息发送到字典。或者,或者使用 CFDictionaryGetValue 代替(CFMutableDictionary 是 CFDictionary 的子类)。
这将有助于告诉我们:
猜测,您可以通过记录对象的
类
来检查Voltage键的对象是否确实是一个字符串。它可能已经是一个数字了。可以合理地假设任何需要 NSString 的对象都会尝试将 NSString 消息发送到您给它的任何对象,因此,如果您给它一个 NSNumber,您将让它发送 NSString 消息到 NSNumber 对象,这是造成这种情况的一个可能原因例外。That's because the compiler doesn't know that CFMutableDictionaries are also NSMutableDictionaries. You'll have to use an explicit cast to tell the compiler that sending messages to the dictionary is OK. Either that, or use
CFDictionaryGetValue
instead (CFMutableDictionary is a subclass of CFDictionary).It would help to tell us:
At a guess, you might check whether the object for the Voltage key really is a string by logging the object's
class
. It might already be a number. It's reasonable to assume that anything that expects an NSString will try to send NSString messages to whatever you give it, so if you give it an NSNumber, you'll have it sending NSString messages to an NSNumber object, which is one possible cause of that exception.