我正在尝试研究使用restKit从foursquare获取一些数据。
我正在使用本教程。
使用此代码
我尝试做一些不同的事情,所以我尝试从类别中获取数据。
这是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);
但它总是为空
我想可能是数据类型中的问题?
有三个名字,我不知道如何让它们全部打印出来。
对不起,我的母语不是英语,所以我不能很好地解释这种情况。