我有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: (
)
答案 0 :(得分:0)
关于Javito_009评论,而不是:
badgesEarned = [(NSArray*)loadedBadgesEarned mutableCopy];
使用以下方法解决了这个问题:
[badgesEarned addObjectsFromArray:loadedBadgesEarned];
答案 1 :(得分:0)
您似乎没有将数组存储回用户首选项,至少您没有显示任何代码。
在某些时候你必须写
[defaults setObject:badgesEarned forKey:@"badgesEarned"];
顺便说一句。您正在不必要地创建可变数组。只需写下
NSMutableArray* badgesEarned = [[defaults valueForKey:@"badgesEarned"] mutableCopy];