2015-01-24 5 views
2

나는 생각할 수있는 모든 것을 시도했으며,이 오류를 수정하기위한 모든 제안을 시도했다. 앱이 iOS 시뮬레이터에서 제대로 실행되지만 실제 기기 (내 iPhone 5)에서 충돌합니다. 이 클래스의 특성변하기 쉬운 메소드가 물리적 객체에서만 발생하는 불변 객체로 전송 됨

2015-01-23 16:26:43.232 Kyle's App[2343:773183] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' 

선언은 다음과 같습니다 :

@property (strong, nonatomic) NSUserDefaults *defaults; 

@property (strong, nonatomic) NSMutableArray *classes; 

@property (strong, nonatomic) NSMutableArray *foundClasses; 

이 모든

가 변경 가능한 객체로 초기화 사용자 기본값 을 제외하고 여기에

는 오류가 내가 얻을 수있다 오류가 발생하는 방법 :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSMutableArray *notes = [[NSMutableArray alloc] initWithArray:self.classes[indexPath.row][@"notes"]]; 
    [notes addObject:[[self.defaults objectForKey:@"New Note"] mutableCopy]]; 

    self.classes[indexPath.row][@"notes"] = [notes mutableCopy];//problem 

    [self.defaults setObject:@(indexPath.row) forKey:@"Current Class"]; 

    [self.defaults setObject:self.classes forKey:@"Classes"]; 
    [self.defaults setObject:[self.defaults objectForKey:@"New Note"] forKey:@"Current Note"]; 
    [self.defaults setObject:@YES forKey:@"Created New Note"]; 
    [self.defaults synchronize]; 

    [self performSegueWithIdentifier:@"Show Summary" sender:self]; 
} 

오류가 발생하는 경우 self.classes[indexPath.row][@"notes"] = [notes mutableCopy];//problem

모든 도움을 주셔서 감사 드리며, 잘못된 것이 있으면 사과드립니다. 처음 StackOverflow에 게시하는 것이므로 잘못되었습니다. 더 자세한 정보가 필요하면 알려주십시오.

감사합니다.

업데이트 - 사전에 초기화 된 코드가 있는데 어떻게 여전히 불변인지 알 수 없습니다.

[self.classes addObject:(NSMutableDictionary *)@{ 
           @"title": textField.text, 
           @"notes": [[NSMutableArray alloc] init]//(NSMutableArray *)@[] 
           }]; 
+2

여기서 'self.classes'에 사전을 입력하는 코드는 어디에 있습니까? 'NSDictionary'가 아닌'NSMutableDictionary'을 추가 할 필요가 있습니다. – rmaddy

+0

'노트'배열이 변경 가능한 배열이므로 'mutableCopy'메서드가 불필요합니다 – stosha

+0

이것을보십시오'self.classes [indexPath.row] [@ "notes"] = [노트 사본]; – Saif

답변

1

당신이 당신의 배열 classesNSDictionary의 변경 가능한 버전 (NSMutableDictionary, NSCache 등)을 넣어해야합니다.

[self.classes addObject:[NSMutableDictionary dictionary]]; 

편집 : 그것은 그 변경 가능한 동등한 불변의 객체를 변환 할 수있는 올바른 방법이 아니다. 전송하는 대신 [mutableCopy]으로 전화해야합니다.

[self.classes addObject:[@{ 
          @"title": textField.text, 
          @"notes": [[NSMutableArray alloc] init]} mutableCopy]]; 
+0

객체를 형 변환하려고했습니다. , 내가 위에 게시 한 나는 아직도 오류를지고있다. 어떤 제안? – kasandell

+0

개체를 변형하여 @ozgurv로 만들려면 어떻게해야합니까? – kasandell

+0

@kasandell 당신은'[object mutableCopy]'메시지를 수신자에게 보낼 수 있습니다. – ozgur