2012-08-05 2 views
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 

답변

2

나는이 방법의 말에, 사용자 기본으로, 사전 인 recentPhoto를 저장하려고하면 문제가 생각입니다 recentPhotos 대신 저장하려는 변경 가능한 배열.

실제로는 recentPhoto이 사용자 기본값에 저장되지 않았으므로이 메서드가 처음 호출 될 때 충돌이 발생하지 않을 것이라고 생각합니다. 하지만 그 후에는 그렇게 될 것입니다.

+0

하지만 첫 번째 NSLog를 수행 할 때 메소드가 충돌합니다. –

+1

예, 사용자 기본값에서 가져온 것이 사전이 아니기 때문에 objectAtIndex : 메소드가 없습니다. – Selkie

+0

내가 사용하는 앱 뒤에 어떤 코드가 있는지 궁금해 할 때가 이런 때가 ... – jrtc27