Question

Problem: I'm trying to retrieve an NSNumber from a dictionary but the long value stored in the dictionary is cleared to 0 upon retrieval.

The code below passes the NSNumber variable time_of_update into the dictionary. time_of_update has a long value that comes from a timestamp and is never zero. The problem arises when trying to read time_of_update back from the dictionary as an NSNumber than converting it to a long. The result is always zero instead of the timestamp value it was initially set to.

NSMutableDictionary *_update = [NSMutableDictionary dictionaryWithObjectsAndKeys:time_of_update,@"time",nil];
NSLog(@"Output: %ld",[[_update objectForKey:@"time"] longValue]);
Was it helpful?

Solution

I bet something else earlier in your dictionary is nil. When that happens, everything that follows ends up being ignored. So in your next line, [_update objectForKey:@"time"] is probably nil, which turns the long into 0.

When you NSLog() [_update objectForKey:@"time"] is it nil?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top