Reputation: 3672
The following code snippet:
NSLog(@"userInfo: The timer is %d", timerCounter);
NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:timerCounter] forKey:@"timerCounter"];
NSUInteger c = (NSUInteger)[dict objectForKey:@"timerCounter"];
NSLog(@"userInfo: Timer started on %d", c);
produces output along the lines of:
2009-10-22 00:36:55.927 TimerHacking[2457:20b] userInfo: The timer is 1
2009-10-22 00:36:55.928 TimerHacking[2457:20b] userInfo: Timer started on 5295968
(FWIW, timerCounter is a NSUInteger.)
I'm sure I'm missing something fairly obvious, just not sure what it is.
Upvotes: 14
Views: 22550
Reputation: 71
Or like this with literals:
NSUInteger c = ((NSNumber *)dict[@"timerCounter"]).unsignedIntegerValue;
You must cast as NSNumber first as object pulled from dictionary will be id_nullable and so won't respond to the value converting methods.
Upvotes: 0
Reputation: 237070
Dictionaries always store objects. NSInteger and NSUInteger are not objects. Your dictionary is storing an NSNumber (remember that [NSNumber numberWithInteger:timerCounter]
?), which is an object. So as epatel said, you need to ask the NSNumber for its unsignedIntegerValue
if you want an NSUInteger.
Upvotes: 11
Reputation: 46041
You should use intValue
from the received object (an NSNumber
), and not use a cast:
NSUInteger c = [[dict objectForKey:@"timerCounter"] intValue];
Upvotes: 23