我想从 CFMutableDictionaryRef 访问一些值,然后对它们进行一些数学运算。使用下面的代码,我设法打印了电池的一些属性。我还能够打印出单个值(尽管这样做我得到一个警告:invalid receiver type: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 );