下面的代碼片段:如何在NSDictionary中正確設置整數值?
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);
線沿線的產生輸出:(FWIW,timerCounter是NSUInteger)
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
我敢肯定,我失去了一些東西相當明顯,只是不確定它是什麼。
實際上,對於NSUInteger,你應該使用'-unsignedIntegerValue'。 – 2009-10-22 06:14:25
(和'+ [NSNumber numberWithUnsignedInteger:]'創建NSNumber,如果它確實是無符號的。) – Wevah 2009-10-22 21:54:21
你可以用'@(timerCounter)'代替'[NSNumber numberWithInteger:timerCounter]' – fpg1503 2016-12-19 02:58:56