2012-04-24 15 views
6

İç içe geçmiş bir dizinin eşlenmesiyle ilgili bir sorum var. https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md'da iç içe nesnelerin eşlenmesi için bir örnek var.Restkit Eşleme İç İçe Dizilim

{ "articles": [ 
    { "title": "RestKit Object Mapping Intro", 
     "body": "This article details how to use RestKit object mapping...", 
     "author": [{ 
      "name": "Blake Watters", 
      "email": "[email protected]" 
     }, 
     { 
      "name": "abc", 
      "email": "emailaddress" 
     }] 
     "publication_date": "7/4/2011" 
    }] 
} 

nasıl sınıfları Yazarlar bir dizi almak için, aşağıdaki gibi görünmelidir: yuvalanmış nesne JSON benziyor örneğin nesneler dizisi, eğer

Ama yapmak ne var? Örneğin kod Thats :

@interface Author : NSObject 
    @property (nonatomic, retain) NSString* name; 
    @property (nonatomic, retain) NSString* email; 
@end 



@interface Article : NSObject 
    @property (nonatomic, retain) NSString* title; 
    @property (nonatomic, retain) NSString* body; 
    @property (nonatomic, retain) Author* author; // should be an array of Author-Objects 
    @property (nonatomic, retain) NSDate* publicationDate; 
@end 

Ben Yazarlar bir Array gidecekseniz ve ben NSArray Madde yazar Özellik sınıfını değiştirme ediyorsam, nasıl yaptığını Restkit o bilir Restkit, söyleyebilir nasıl Bu NSArray Yazar Nesneleri olmalıdır ...?

Nesneleri JSON'dan Objective-c ve tersine eşlemek için RKObjectMapping kullanıyorum.

cevap

0

Tüm respnse dize .. o zaman yazar değerleri dizisi namutable atamak nsmutable sözlük olarak götürün

Sen buna göre var türü ayarlamak emin olmak istersiniz
7

:

@interface Article : NSObject 
    @property (nonatomic, retain) NSString* title; 
    @property (nonatomic, retain) NSString* body; 
    @property (nonatomic, retain) NSSet* authors; 
    @property (nonatomic, retain) NSDate* publicationDate; 
@end 

O may NSArray olarak ilan etmekle çalışıyorum ama ben şahsen RestKit'i CoreData modelleri ile kullanıyorum ve bu senaryolardaki ilişkiler NSSet'tir.

Ayrıca, eşleştirmeyi kurmak gerekir:

[articleMapping mapKeyPath:@"author" toRelationship:@"authors" withMapping:authorMapping]; 
3

Bölme dışarı yardımcı olur.

@interface Author : NSObject 
    @property (nonatomic, retain) NSString* name; 
    @property (nonatomic, retain) NSString* email; 
@end 



@interface Article : NSObject 
    @property (nonatomic, retain) NSString* title; 
    @property (nonatomic, retain) NSString* body; 
    @property (nonatomic, retain) NSArray* author; 
    @property (nonatomic, retain) NSDate* publicationDate; 
@end 


//create the article mapping 
RKObjectMapping *articleMapping = [RKObjectMapping mappingForClass:[Article class]]; 
//add rest of mappings here 
[articleMapping addAttributeMappingsFromDictionary:@{ 
                @"title":@"title" 
} 

//create the author mapping 
RKObjectMapping *authorMapping = [RKObjectMapping mappingForClass:[Author class]]; 
//add rest of mappings here 
[authorMapping addAttributeMappingsFromDictionary:@{ 
                @"name":@"name" 
} 

//link mapping with a relationship 
RKRelationshipMapping *rel = [RKRelationshipMapping relationshipMappingFromKeyPath:@"authors" toKeyPath:@"author" withMapping:authorMapping]; 

//add relationship mapping to article 
[articleMapping addPropertyMapping:rel];