1
안녕하세요 NSUserDefaults에 배열을 저장하려고하는데 안녕하세요. 문제가 있습니다. 이 메소드는 NSUSerDefaults에 저장할 배열에 저장할 NSDictionary를 받아들입니다. 문제는 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를 수행 할 때 메소드가 충돌합니다. –
예, 사용자 기본값에서 가져온 것이 사전이 아니기 때문에 objectAtIndex : 메소드가 없습니다. – Selkie
내가 사용하는 앱 뒤에 어떤 코드가 있는지 궁금해 할 때가 이런 때가 ... – jrtc27