NSMutableArray 添加元素不起作用
NSMutableArray add element not working
我有一个 NSMutableArray
并向其中添加了一个新元素。但显然它没有被添加。我在这里做错了什么?有什么提示吗?但是,当我 运行 第二次使用这个程序时,它运行正常。
代码:
NSMutableArray *badgesEarned;
badgesEarned = [[NSMutableArray alloc]init];
NSArray *loadedBadgesEarned = [defaults valueForKey:@"badgesEarned"];
badgesEarned = [(NSArray*)loadedBadgesEarned mutableCopy];
NSLog(@"start loaded badges: %@",badgesEarned);
NSArray *type = @"typeABC1";
[badgesEarned addObject:type];
NSLog(@"handleBadge type: %@",type);
NSLog(@"handleBadge result: %@",badgesEarned);
控制台输出 - 第一个 运行:
2015-04-24 12:55:12.664 appName[5206:607] start loaded badges: (null)
2015-04-24 12:55:12.664 appName[5206:607] handleBadge type: typeABC1
2015-04-24 12:55:12.664 appName[5206:607] handleBadge result: (null)
控制台输出 - 第二个 运行:
2015-04-24 13:16:58.133 appName[5305:607] start loaded badges: (null)
2015-04-24 13:16:58.133 appName[5305:607] handleBadge type: typeABC1
2015-04-24 13:16:58.133 appName[5305:607] handleBadge result: (
typeABC1
)
补充信息:
NSLog(@"start loadedBadgesEarned: %@",loadedBadgesEarned);
给出:
第一 运行:
2015-04-24 13:57:35.473 appName[6090:607] start loadedBadgesEarned: (null)
第二 运行:
2015-04-24 13:57:49.418 appName[6090:607] start loadedBadgesEarned: (
)
关于 Javito_009 评论,而不是:
badgesEarned = [(NSArray*)loadedBadgesEarned mutableCopy];
使用以下解决问题:
[badgesEarned addObjectsFromArray:loadedBadgesEarned];
看来您没有将数组存储回用户首选项,至少您没有显示任何代码。
在某些时候你必须写
[defaults setObject:badgesEarned forKey:@"badgesEarned"];
顺便说一句。您正在不必要地创建一个可变数组。随便写
NSMutableArray* badgesEarned = [[defaults valueForKey:@"badgesEarned"] mutableCopy];
我有一个 NSMutableArray
并向其中添加了一个新元素。但显然它没有被添加。我在这里做错了什么?有什么提示吗?但是,当我 运行 第二次使用这个程序时,它运行正常。
代码:
NSMutableArray *badgesEarned;
badgesEarned = [[NSMutableArray alloc]init];
NSArray *loadedBadgesEarned = [defaults valueForKey:@"badgesEarned"];
badgesEarned = [(NSArray*)loadedBadgesEarned mutableCopy];
NSLog(@"start loaded badges: %@",badgesEarned);
NSArray *type = @"typeABC1";
[badgesEarned addObject:type];
NSLog(@"handleBadge type: %@",type);
NSLog(@"handleBadge result: %@",badgesEarned);
控制台输出 - 第一个 运行:
2015-04-24 12:55:12.664 appName[5206:607] start loaded badges: (null)
2015-04-24 12:55:12.664 appName[5206:607] handleBadge type: typeABC1
2015-04-24 12:55:12.664 appName[5206:607] handleBadge result: (null)
控制台输出 - 第二个 运行:
2015-04-24 13:16:58.133 appName[5305:607] start loaded badges: (null)
2015-04-24 13:16:58.133 appName[5305:607] handleBadge type: typeABC1
2015-04-24 13:16:58.133 appName[5305:607] handleBadge result: (
typeABC1
)
补充信息:
NSLog(@"start loadedBadgesEarned: %@",loadedBadgesEarned);
给出:
第一 运行:
2015-04-24 13:57:35.473 appName[6090:607] start loadedBadgesEarned: (null)
第二 运行:
2015-04-24 13:57:49.418 appName[6090:607] start loadedBadgesEarned: (
)
关于 Javito_009 评论,而不是:
badgesEarned = [(NSArray*)loadedBadgesEarned mutableCopy];
使用以下解决问题:
[badgesEarned addObjectsFromArray:loadedBadgesEarned];
看来您没有将数组存储回用户首选项,至少您没有显示任何代码。
在某些时候你必须写
[defaults setObject:badgesEarned forKey:@"badgesEarned"];
顺便说一句。您正在不必要地创建一个可变数组。随便写
NSMutableArray* badgesEarned = [[defaults valueForKey:@"badgesEarned"] mutableCopy];