views:

109

answers:

1

Hello Everyone,

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 );
+1  A: 

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.

Peter Hosey