开发者

getting the value through key in the dictionary

开发者 https://www.devze.com 2023-03-08 01:39 出处:网络
I need to get the value for the particular key in the map, id val = [m_cAppIdMap valueForKey:wTimerIds];

I need to get the value for the particular key in the map,

id val = [m_cAppIdMap valueForKey:wTimerIds];

where wTimerIds is a unsigned short integer.

Runtime exception:EXC_BAD开发者_如何学运维_ACCESS

Warning: passing argument 1 of 'valueForKey:' makes pointer from integer without a cast

EDITED:

 NSNumber* val = [m_cAppIdMap valueForKey:[NSNumber numberWithUnsignedShort:wTimerIds]];

The warning i'm getting is, incompatible Objective-C types 'struct NSNumber *', expected 'struct NSString *' when passing argument 1 of 'valueForKey:' from distinct Objective-C type

The runtime exception i'm getting is, [NSCFNumber length]: unrecognized selector sent to instance 0x100110c20

I am storing the the key as a unsignedshort in the dictioanry.so i'm trying to search for the value in the dictionary with that data type.


You can not have primitive integer as key. Keys must be object. You can wrap that integer to a NSString to use as a key.

NSString *key = [NSString stringWithFormat:@"%d", wTimerIds];
[m_cAppIdMap setValue:val forKey:key];
id val = [m_cAppIdMap valueForKey:key];

Or you can wrap that in NSNumber too. You need to use the same thing for both setting and getting. That is, if you use string in setValue:forKey: then you need to use string for valueForKey:. Similarly if you use NSNumber then you need to use that in both place.


You should use

NSNumber *wTimerIdsNum = [NSNumber numberWithInt:wTimerIds];
id val = [m_cAppIdMap objectForKey:wTimerIdsNum];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号