获取 CFMutableDictionaryRef 值

发布于 2024-09-11 02:20:56 字数 1006 浏览 10 评论 0原文

我想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏剧牡丹亭 2024-09-18 02:20:57

我还能够打印出单个值(尽管这样做时我收到警告:无效的接收器类型:CFMutableDictionaryRef)。

那是因为编译器不知道 CFMutableDictionaries 也是 NSMutableDictionaries。您必须使用显式强制转换来告诉编译器可以将消息发送到字典。或者,或者使用 CFDictionaryGetValue 代替(CFMutableDictionary 是 CFDictionary 的子类)。

然后我尝试将字符串值转换为 NSNumber,然后程序出现错误。它给了我类似发送到实例 0x5d65f70 的无法识别的选择器

这将有助于告诉我们:

  • 哪个选择器未被识别
  • 0x5d65f70 当时是什么类型的对象
  • 程序的哪一行导致了异常(如果您这样做,调试器会告诉您这一点)在其下运行您的应用程序)

猜测,您可以通过记录对象的来检查Voltage键的对象是否确实是一个字符串。它可能已经是一个数字了。可以合理地假设任何需要 NSString 的对象都会尝试将 NSString 消息发送到您给它的任何对象,因此,如果您给它一个 NSNumber,您将让它发送 NSString 消息到 NSNumber 对象,这是造成这种情况的一个可能原因例外。

I am also able to print out single values (although in doing so I get a warning:invalid receiver type:CFMutableDictionaryRef).

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).

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

It would help to tell us:

  • What selector wasn't recognized
  • What kind of object 0x5d65f70 was at the time
  • What line of your program caused the exception (the debugger will tell you this if you run your application under it)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文