2012-11-29 2 views
2

I했습니다 어려움 매핑 특정한 경우 이 정확히 내가 작업 개체에 대한 매핑을 생성 hereRestKit : 없음 ID로 중첩 된 객체와 매핑 (dupplicated)

product:{ 
    "id": 123, 
    "name": "Produce RestKit Sample Code", 
    "description": "We need more sample code!", 
    "tasks": [ 
     {"name": "Identify samples to write", "assigned_user_id":1}, 
     {"name": "Write the code", "assigned_user_id": 1}, 
     {"name": "Push to Github", "assigned_user_id": 1}, 
     {"name": "Update the mailing list", "assigned_user_id": 1}] 
} 

같은 문제입니다. NSSET 작업과의 관계가있는 제품 개체에 대한 매핑을 만들었습니다.

하지만 이제는 새 데이터를 구문 분석 할 때마다 작업이 핵심 데이터에 복제됩니다. (일반 원인 ID 없음)

견인 솔루션 :

  1. 새가 발견되면 나는 현재 제품의 작업을 삭제할 수 있습니다.
  2. 는 나는이 솔루션 중 하나를 구현하는 방법을 모르는 제품 번호

를 사용하여 작업의 ID를 생성 할 수 있습니다. 어떤 도움이라도 좋을 것입니다. 나는 당신의 매핑이 작업 개체,하지만 난을 통해 JSON 문자열로 NSData을 구문 분석하는 방법을 아주 확실하지 않다

답변

1

:이 경우

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data 
                  options:0 
                  error:&error]; 

, 나는 "제품"는 NSDictionary 하나 열쇠를 가져와 그 키에 대한 객체는 다른 사전입니다. "tasks"키가있는 NSDictionary의 개체는 개의 개체 중 NSArray입니다.

이제는 JSON 발췌문이 유효한 JSON이 아니었지만 더 광범위한 JSON 파일의 일부라고 가정합니다. 그러나 테스트 목적 들어, 다음과 같이 JSON 파일이었다 가정 해 봅시다 :

{ 
    "product" : { 
     "id": 123, 
     "name": "Produce RestKit Sample Code", 
     "description": "We need more sample code!", 
     "tasks": [ 
        {"name": "Identify samples to write", "assigned_user_id": 1}, 
        {"name": "Write the code", "assigned_user_id": 1}, 
        {"name": "Push to Github", "assigned_user_id": 1}, 
        {"name": "Update the mailing list", "assigned_user_id": 1}] 
    } 
} 

가 그럼 난 구문 분석 할 수 JSON과 같이 : 또는

NSString *filename = [[NSBundle mainBundle] pathForResource:@"13628140" ofType:@"json"]; 
NSData *data = [NSData dataWithContentsOfFile:filename]; 
NSError *error; 
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data 
                  options:0 
                  error:&error]; 

NSDictionary *product = dictionary[@"product"]; 
NSArray *tasks = product[@"tasks"]; 
NSDictionary *firstTask = tasks[0]; 
NSString *firstName = firstTask[@"name"]; 
NSString *firstAssignedUserId = firstTask[@"assigned_user_id"]; 

또는, 당신이 작업을 통해 열거하려면 :

NSDictionary *product = dictionary[@"product"]; 
NSArray *tasks = product[@"tasks"]; 

[tasks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
    NSDictionary *task = obj; 
    NSLog(@"Task \"%@\" is assigned to %@", task[@"name"], task[@"assigned_user_id"]); 
}]; 

당신은 코어 데이터에서 그 NSArray tasks의를 저장하는 방법을 묻는?

+0

답변 해 주셔서 감사합니다. 사실, 나는 "작업"을 분석 할 수 있었지만 문제는 중복 된 작업을 만들지 않고 Coredata에 저장하는 것이 었습니다. 지금은 개체 로더의 "willMapData"대리인을 가로 채고 구문 분석 된 "제품"의 모든 "작업"을 삭제 한 다음 새 개체를 추가합니다. 그것은 작동하지만, 내가 원하는 것처럼 깨끗하지는 않습니다. – Vassily