从字典中删除 NSNumber 会持续返回长值 0

问题描述 投票:0回答:1

问题:我试图从字典中检索 NSNumber,但存储在字典中的 long 值在检索时被清除为 0。

下面的代码将 NSNumber 变量

time_of_update
传递到字典中。
time_of_update
具有来自时间戳的长值并且永远不为零。当尝试从字典中将
time_of_update
作为 NSNumber 读回而不是将其转换为 long 时,就会出现问题。结果始终为零,而不是最初设置的时间戳值。

NSMutableDictionary *_update = [NSMutableDictionary dictionaryWithObjectsAndKeys:time_of_update,@"time",nil];
NSLog(@"Output: %ld",[[_update objectForKey:@"time"] longValue]);
objective-c nsdictionary nsnumber
1个回答
2
投票

我敢打赌,你字典中之前的其他内容是零。当这种情况发生时,接下来的一切都会被忽略。所以在你的下一行中, [_update objectForKey:@"time"] 可能是 nil,这会将 long 变成 0。

当你 NSLog() [_update objectForKey:@"time"] 是 nil 吗?

© www.soinside.com 2019 - 2024. All rights reserved.