2013-07-22 3 views
9

내 IOS 응용 프로그램에서 데이터베이스의 사용자 정보를 업데이트하려고합니다 (Stackmob 포함). "인식 할 수없는 선택기가 인스턴스로 전송되었습니다." 사전에iOS 및 Stackmob - [NSNull 길이] : 인스턴스로 전송 된 인식 할 수없는 선택 자

-[NSNull length]: unrecognized selector sent to instance 0x1ec5678 

2013-07-21 12:01:25.773 [29207:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[NSNull length]: unrecognized selector sent to instance 0x1ec5678' 

감사 :

- (IBAction)save:(UIButton *)sender { 

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"User"]; 
NSPredicate *predicte = [NSPredicate predicateWithFormat:@"username == %@", self.username]; 
[fetchRequest setPredicate:predicte]; 

[self.managedObjectContext executeFetchRequest:fetchRequest onSuccess:^(NSArray *results) { 

    NSManagedObject *todoObject = [results objectAtIndex:0]; 
    [todoObject setValue:@"[email protected]" forKey:@"email"]; 

    [self.managedObjectContext saveOnSuccess:^{ 
     NSLog(@"You updated the todo object!"); 
    } onFailure:^(NSError *error) { 
     NSLog(@"There was an error! %@", error); 
    }]; 

} onFailure:^(NSError *error) { 

    NSLog(@"Error fetching: %@", error); 

}]; 
} 

는 여기에 내가지고있어 전체 오류입니다.

+1

일부는 Nil 값 또는 개체를 가져오고이 오류가 발생한 이유를 nil로 설정하려고 시도합니다. befor setValue가 0이거나 아닌지 확인하십시오. –

+0

가능한 복제본 [- \ [NSNull length \] : 인식 할 수없는 선택기로 전송 ... 메모리 누수?] (http://stackoverflow.com/questions/16607960/nsnull-length-unrecognized-selector-sent-to- a-memory-leak) – borrrden

+0

self.username을 인쇄하고 포함 된 내용을 볼 수 있습니까? – satheeshwaran

답변

42

당신의 self.username이 비어 있기 때문일 수도 있습니다. 당신은 JSON에서 사용자 이름 데이터를 가져 오는 경우, 당신은 JSON 인터프리터는 NSNull 객체를 생성하기 때문에 빈 데이터를 피하기 위해 if(username){...}하지만

if(![username isKindOfClass:[NSNull class]]) 

을 사용할 수 없습니다.

+2

이 솔루션은 작동하지만 나는 더 나은 해결책을 찾았지만 http://stackoverflow.com/questions/16607960/nsnull-length-unrecognized-selector-sent-to-a-memory-leak/16610117 –

+0

@ 특정 상황에서 작동 할 수 있습니다. 제작 과정에서 사용하는 것은 매우 위험한 전략입니다. – dmur

+0

하지만 JSON과 관련이 있습니까? – user102008