2014-12-26 2 views
0

내 모델에 NSString 속성을 설정하면 즉시 Null로 반환됩니다. 이상한 점은 typedef 속성을 설정하면 유지된다는 것입니다 (예 : NSInteger). 코드는 다음과 같습니다. 나는 또한 GCD를 통해이 방법을 실행하고있다. 그러나 나는 이것을 주석 처리했다. & 그것은 아무 것도 바뀌지 않았다. 또한 모델에 NSCoding 메서드가 있지만 대리자를 주석 처리해도 아무 것도 변경되지 않았습니다.모델 속성은 JSON에서 설정 한 후 즉시 할당 해제됩니다.

typedef가 아닌 모델 속성을 설정하는 경우를 제외하면 말 그대로 JSON dict 및 NSLogging 결과에 액세스하는 것을 포함하여 모든 것이 작동합니다. 어떤있는 NSString 할당 업데이트

[사전 valueForKey는 "FIRST_NAME는"@] NULL을 반환 그래서 분명히이 사전에 액세스하는 데 문제가 있습니다. 2

업데이트를 사전에 로그인 한 후, 나는 "일부 keyss는"없는 "일부 값이 없습니다"고 알았어하고있다. 나는 레일에서 as_json를 사용하고, 그리고 우체부 크롬 확장이 나던

{ 
available = 1; 
boxing = 0; 
certificates = ""; 
city = A; 
"created_at" = "2014-12-14T07:23:35.445Z"; 
description = "Short description"; 
"drivers_license" = CA12345; 
"drivers_license_expiration" = "12/20"; 
"drivers_license_state" = CA; 
email = "[email protected]"; 
"facebook_id" = ""; 
"first_name" = TEsty; 
gender = ""; 
id = 1; 
"last_name" = Again; 
latitude = "<null>"; 
longitude = "<null>"; 
password = g; 
pilates = 0; 
"reset_password_token" = "<null>"; 
running = 0; 
strength = 0; 
"updated_at" = "2014-12-25T08:42:27.122Z"; 
yoga = 0; 
} 

.H 파일 일어날 등

@interface FSTrainer : NSObject <NSCoding> 

@property (nonatomic) NSInteger id; 
@property (nonatomic, strong) NSString* first_name; 
@property (nonatomic, strong) NSString* last_name; 

- (id) init; 
- (id) initWith:(NSMutableDictionary *)dictionary; 

..

하는 .m 파일

-(id)init{ 
self = [super init]; 
if (!self) return nil; 
return self; 
} 

- (id) initWith:(NSMutableDictionary *)dictionary 
{ 
self = [super init]; 
if (!self) return nil; 


    if ([dictionary valueForKey:@"id"] != [NSNull class]) 
    {self.id = [[dictionary valueForKey:@"id"] integerValue];} 

    if ([dictionary valueForKey:@"first_name"] != [NSNull class]) 
    { 
     [self setFirst_name:[dictionary valueForKey:@"first_name"]]; 

     NSLog(@"%@", [dictionary objectForKey:@"first_name"]); 
     NSLog(@"%@", [dictionary valueForKey:@"first_name"]);} 
     //Both NSLogs print out the correct names 

    if ([dictionary valueForKey:@"last_name"] != [NSNull class]) 
    {self.last_name = [valueForKey objectForKey:@"last_name"];} 

etc.... 

return self; 
} 

다음과 같이 호출됩니다.

dispatch_async(dispatch_get_global_queue(0, 0), ^{ 
     [self computationForRails]; 
    }); 

그리고 기능은

-(void)computationForRails{ 
... 
    FSTrainer* currentTrainer = [[FSTrainer alloc] initWith:[tempdict valueForKey:@"data"]]; 
    //sol-added JNKeychian 
    [JNKeychain saveValue:currentTrainer forKey:@"current_trainer"]; 
} 
tempdict 같

나중에 모델에 접근하고
id jsonObject = [NSJSONSerialization JSONObjectWithData: data 
                options: NSJSONReadingMutableContainers 
                 error: &error]; 

(I 저장 모델 설정하지 않습니다 (데이터 사전의 핵심이다)의 속성이 올바르게 응용 프로그램이 충돌하는 곳입니다.

FSTrainer* current_trainer = [JNKeychain loadValueForKey:@"current_trainer"]; 
    NSString* trainerName = current_trainer.first_name; 
+0

어떻게 속성을 해제지고 당신이 알 수 있습니까? 그 일을 보여 주시겠습니까? –

+0

@AaronBrager 할당이 취소되지 않습니다. 내가 문제를 디버깅 할 때 게시했습니다. 이 속성은 결코 설정되지 않습니다. 임시 NSString을 dicitonary 값으로 설정하려고 시도하고 null을 반환합니다. 하지만 키 (first_name)에 대한 nslog 값이 올바르게 로그 아웃됩니다. 나는 그 방침이 설정되는 방법을 추측하고있다. 내 업데이트를보고 제목을 업데이트하려고합니다. – DaynaJuliana

+0

무엇을 의미합니까? 프로퍼티 세터는'void' 메쏘드입니다. 변수에 액세스하려는 방법을 보여줄 수 있습니까? –

답변

1

코드 바꾸기

이 코드
[self setFirst_name:[dictionary valueForKey:@"first_name"]]; 

[self setFirst_name:[NSString stringWithFormat:@"%@",[dictionary valueForKey:@"first_name"]]]; 

나를 위해 그 작품은

+0

행복한 명절! 불행히도 그게 작동하지 않았지만, 내가 정확한 값을 로그 아웃했다는 것을 알았을 때. 이것은 매우 이상합니다. – DaynaJuliana

+0

@DaynaJuliana - 내 코드를 제발 공유 할 수 있습니까? 따라서 코드를 디버깅하여 문제를 확인할 수 있습니다. –

+0

실제로 이것은 이상하게오고있는 다른 값들에 대해 작동했습니다. 문제는 제가 어떻게 접근했는지입니다. 감사 ! – DaynaJuliana

관련 문제