Objective-C valueforKey

I am using NSDictionary to store given values ​​and I am trying to assign them to a double. However, I'm not sure what to do, valueforkey returns an identifier that is a pointer. I tried using '*' to get the actual value, but this does not work.

This is the code

tempBrain.operand = [variables valueForKey:[(NSString*)obj  stringByReplacingOccurencesOfString:VARIABLE_PREFIX  withString:@""]];

tempBrain.operand is the setter method for a double variable. Variables are my NSDictionary.

+3
source share
3 answers

Assuming the stored value is NSNumber, you can use doubleValueas follows:

tempBrain.operand = [[variables objectForKey:[(NSString*)obj  stringByReplacingOccurencesOfString:VARIABLE_PREFIX  withString:@""]] doubleValue];

Notice that I changed valueForKeyto objectForKey, which is the correct method to get the object from NSDictionary.

+6

NSDictionary NSObject.

:

NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:10];

// Add object
[dic setObject:[NSNumber numberWithDouble:3.0] forKey:@"Third"];

// Read object
double val = [[dic objectForKey:@"Third"] doubleValue];
+1

, , doubleValue.

0
source

Source: https://habr.com/ru/post/1782132/


All Articles