I'm trying to study on using restKit to get some data from foursquare.
I'm using this tutorial.
Using this code
I try to make some different, so I try to get the data from category.
Here is foursqure's json looks like
{
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"
}
}
],
}
}
and here is a part the tutorial that get stats
@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;
mapping part
RKObjectMapping *statsMapping = [RKObjectMapping mappingForClass:[Stats class]];
[statsMapping addAttributeMappingsFromArray:@[@"checkinsCount", @"tipCount", @"usersCount"]];
RKRelationshipMapping *statsRelation = [RKRelationshipMapping relationshipMappingFromKeyPath:@"stats" toKeyPath:@"stats" withMapping:statsMapping];
[venueMapping addPropertyMapping:statsRelation];
after mapping.
I can easily get data as following
NSLog(@"%i",venue.stats.checkinsCount);
and it will print the checkinsCount number
now I try to get the category data. I fix the code to get the new data.
add something like this
@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;
add mapping like this
RKObjectMapping *categoriesMapping = [RKObjectMapping mappingForClass:[Categories class]];
[categoriesMapping addAttributeMappingsFromDictionary:@{@"name":@"name",@"pluralName":@"pluralName"}];
RKRelationshipMapping *cate = [RKRelationshipMapping relationshipMappingFromKeyPath:@"categories" toKeyPath:@"categories" withMapping:categoriesMapping];
[venueMapping addPropertyMapping:cate];
now I try to get the category name
NSLog(@"%@",venue.categories.pluralName);
but it always get null
I think maybe problem which is in the data type?
There are three names and I don't know how to get all of them to print out.
Sorry, my native language isn't English, so I can't explain the situation very well.
In the JSON, categories is an array, so the property you're trying to map it into should be:
@property (strong, nonatomic) NSArray *categories;
You logging code then needs to change to iterate the categories:
for (Categories *category in venue.categories) {
NSLog(@"%@", category.pluralName);
}
The class Categories has a misleading name as it isn't really a collection object (as you seem to have originally intended).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With