Ios 更改NSMutableDictionary中NSNumber的值
我一直在试图找出如何更改存储在NSMutableDictionary中的NSNumber的值。我必须使用NSNumber,因为NSDictionary中只能存储对象。这就是我现在所拥有的,它并没有像我预期的那样发挥作用Ios 更改NSMutableDictionary中NSNumber的值,ios,objective-c,nsdictionary,Ios,Objective C,Nsdictionary,我一直在试图找出如何更改存储在NSMutableDictionary中的NSNumber的值。我必须使用NSNumber,因为NSDictionary中只能存储对象。这就是我现在所拥有的,它并没有像我预期的那样发挥作用 int newInt = [self.myDict[@"key"] intValue] + 100; NSLog(@"%d",newInt); [self.myDict setObject:@(newInt) forKey:@"key"]; NSL
int newInt = [self.myDict[@"key"] intValue] + 100;
NSLog(@"%d",newInt);
[self.myDict setObject:@(newInt) forKey:@"key"];
NSLog(@"%d",[self.myDict[@"key"] intValue]);
第一个NSLog按我的预期打印100,但第二个打印0。我应该如何更改该词典中NSNumber的值?谢谢你的帮助 答案是self.myDict为零。记录它,您将看到。您需要:self.myDict=[NSMutableDictionary new];在其中设置值之前 例如:
@interface AppDelegate ()
@property (nonatomic, strong) NSMutableDictionary *myDict;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.myDict = [NSMutableDictionary new];
int newInt = [self.myDict[@"key"] intValue] + 100;
NSLog(@"%d",newInt);
[self.myDict setObject:@(newInt) forKey:@"key"];
NSLog(@"%d",[self.myDict[@"key"] intValue]);
}
self.myDict
是否已初始化?应该可以。您是否已登录self.myDict以确保它不是零?