Reputation: 31
I am using an NSDictionary to store a set values and I am trying to assign them to a double. However I am not sure what to do, valueforkey returns an id which is a pointer. I have tried using the '*' get the actual value but that does not work.
This is the code
tempBrain.operand = [variables valueForKey:[(NSString*)obj stringByReplacingOccurencesOfString:VARIABLE_PREFIX withString:@""]];
tempBrain.operand is a setter method for a double variable. Variables is my NSDictionary.
Upvotes: 3
Views: 3721
Reputation: 31
I get it now, it returns an object so I should send the doubleValue message to it.
Upvotes: 0
Reputation: 2230
NSDictionary can contain only objects of NSObject subclasses.
Try do this:
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:10];
// Add object
[dic setObject:[NSNumber numberWithDouble:3.0] forKey:@"Third"];
// Read object
double val = [[dic objectForKey:@"Third"] doubleValue];
Upvotes: 1
Reputation: 15857
Assuming the value stored is an NSNumber
you can use doubleValue
like this:
tempBrain.operand = [[variables objectForKey:[(NSString*)obj stringByReplacingOccurencesOfString:VARIABLE_PREFIX withString:@""]] doubleValue];
Note also I have changed valueForKey
to objectForKey
, which is the correct method to use to get an object from an NSDictionary
.
Upvotes: 6