문제

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]);
도움이 되었습니까?

해결책

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top