1
嗨我想存储一个数组到NSUserDefaults但我有麻烦。该方法接受一个NSDictionary,我将其存储到一个数组中,该数组将存储到NSUSerDefaults中。问题是当我做一个mutableCopy它说它是一个字典,而不是类型NSMutable数组?这个方法是我第一次调用NSUserDefaults,所以我不确定错误发生的原因是什么?下面是代码感谢NSUserDefaults和存储值
+(void) getRecentPhoto:(NSDictionary *)recentPhoto{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
//stores it as a dictionary? error happens here
NSMutableArray* recentPhotos = [[defaults objectForKey:@"recentPhoto"] mutableCopy];
NSLog(@"%@", [recentPhotos class]);
if(!recentPhotos) recentPhotos = [NSMutableArray array];
BOOL copy = NO;
//these will crash the program
NSLog(@"%@", [[recentPhotos objectAtIndex:0] objectForKey:@"id"]);
NSLog(@"%@", [recentPhoto objectForKey:@"id"]);
//this checks if it has been stored before by using an id key
for(int i =0; i < [recentPhotos count]; i++){
if ([[[recentPhotos objectAtIndex:i] objectForKey:@"id"] isEqualToString:[recentPhoto objectForKey:@"id"]]) {
copy = YES;
}
}
if(copy ==NO)
[recentPhotos addObject:recentPhoto];
[defaults setObject:recentPhoto forKey:@"recentPhoto"];
[defaults synchronize];
}
这是错误
NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector
但是当我执行第一个NSLog时,该方法崩溃 – 2012-08-05 23:09:57
是的,它没有objectAtIndex:方法,因为您从用户获得的默认值是字典,而不是数组。 – Selkie 2012-08-05 23:17:57
当我想知道我使用的某些应用程序的背后是什么代码时,就像这样的时代... – jrtc27 2012-08-05 23:27:53