2013-05-25 207 views
4

我想研究使用restKit從foursquare獲取一些數據。我使用tutorialRestkit映射與Forsquare的嵌套陣列

使用這種code

我嘗試做一些不同的,所以我嘗試從類別獲取數據。

這裏是foursqure的JSON看起來像

{ 
    venue: { 
    id: "40a55d80f964a52020f31ee3", 
    name: "Clinton St. Baking Co. & Restaurant", 
    contact: { 
     phone: "6466026263", 
     formattedPhone: "(646) 602-6263" 
    }, 
    location: { 
     address: "4 Clinton St", 
     crossStreet: "at E Houston St.", 
     lat: 40.721294, 
     lng: -73.983994, 
     postalCode: "10002", 
     city: "New York", 
     state: "NY", 
     country: "United States", 
     cc: "US" 
    }, 
    canonicalUrl: "https://foursquare.com/v/clinton-st-baking-co--restaurant/40a55d80f964a52020f31ee3", 
    categories: [ 
     { 
     id: "4bf58dd8d48988d143941735", 
     name: "Breakfast Spot", 
     pluralName: "Breakfast Spots", 
     shortName: "Breakfast/Brunch", 
     icon: { 
      prefix: "https://foursquare.com/img/categories_v2/food/breakfast_", 
      suffix: ".png" 
     }, 
     primary: true 
     }, 
     { 
     id: "4bf58dd8d48988d16a941735", 
     name: "Bakery", 
     pluralName: "Bakeries", 
     shortName: "Bakery", 
     icon: { 
      prefix: "https://foursquare.com/img/categories_v2/food/bakery_", 
      suffix: ".png" 
     } 
     }, 
     { 
     id: "4bf58dd8d48988d16d941735", 
     name: "Café", 
     pluralName: "Cafés", 
     shortName: "Café", 
     icon: { 
      prefix: "https://foursquare.com/img/categories_v2/food/cafe_", 
      suffix: ".png" 
     } 
     } 
    ], 

    } 
} 

這裏是如下部分:數據包中得到

@interface Venue : NSObject 

@property (strong, nonatomic) NSString *name; 
@property (strong, nonatomic) Stats *stats; 


@interface Stats : NSObject 

@property (nonatomic, strong) NSNumber *checkinsCount; 
@property (nonatomic, strong) NSNumber *tipCount; 
@property (nonatomic, strong) NSNumber *usersCount; 

映射部分

RKObjectMapping *statsMapping = [RKObjectMapping mappingForClass:[Stats class]]; 
    [statsMapping addAttributeMappingsFromArray:@[@"checkinsCount", @"tipCount", @"usersCount"]]; 
    RKRelationshipMapping *statsRelation = [RKRelationshipMapping relationshipMappingFromKeyPath:@"stats" toKeyPath:@"stats" withMapping:statsMapping]; 
    [venueMapping addPropertyMapping:statsRelation]; 

映射後的教程。

我可以很容易地得到數據如下

NSLog(@"%i",venue.stats.checkinsCount); 

,它將打印checkinsCount數

現在我試圖讓該類別的數據。我修復了代碼以獲取新數據。

添加這樣的事情

@interface Venue : NSObject 

@property (strong, nonatomic) NSString *name; 
@property (strong, nonatomic) Categories *categories; 

@interface Categories : NSObject 

@property (nonatomic, strong) NSString *name; 
@property (nonatomic ,strong) NSString *pluralName; 

添加這樣

RKObjectMapping *categoriesMapping = [RKObjectMapping mappingForClass:[Categories class]]; 
    [categoriesMapping addAttributeMappingsFromDictionary:@{@"name":@"name",@"pluralName":@"pluralName"}]; 
    RKRelationshipMapping *cate = [RKRelationshipMapping relationshipMappingFromKeyPath:@"categories" toKeyPath:@"categories" withMapping:categoriesMapping]; 
    [venueMapping addPropertyMapping:cate]; 

映射現在我試圖讓類別名稱

NSLog(@"%@",venue.categories.pluralName); 

,但它總是空

我認爲mayb這是數據類型中的問題嗎?

有三個名字,我不知道如何讓它們全部打印出來。

對不起,我的母語不是英語,所以我無法很好地解釋這種情況。

+0

當你說它不起作用,你是什麼意思?與實際發生的情況相比,你希望發生什麼? – Steve

+0

稍等片刻,我忘了輸入,謝謝 – Exiling

回答

7

在JSON,類別是一個數組,所以物業你想它映射到應該是:

@property (strong, nonatomic) NSArray *categories; 

您登錄代碼,然後需要改變迭代的類別:

for (Categories *category in venue.categories) { 
    NSLog(@"%@", category.pluralName); 
} 

類別類別有一個令人誤解的名稱,因爲它不是一個真正的集合對象(就像您最初似乎打算的那樣)。

+0

我試了一下,但是我打印的數據看起來像是 , 我怎麼能得到類別名?tks – Exiling

+0

明白了,我想我忘了它是一個數組類型。 犯一個愚蠢的錯誤。非常感謝你!! – Exiling

+0

投票分類迭代 –