我想研究使用restKit從foursquare獲取一些數據。我使用tutorial。Restkit映射與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這是數據類型中的問題嗎?
有三個名字,我不知道如何讓它們全部打印出來。
對不起,我的母語不是英語,所以我無法很好地解釋這種情況。
當你說它不起作用,你是什麼意思?與實際發生的情況相比,你希望發生什麼? – Steve
稍等片刻,我忘了輸入,謝謝 – Exiling