Displaying a Restkit Nested Array Using Forsquare

I am trying to learn using restKit to get some data from foursquare.

I am using this tutorial .

Using this code

I'm trying to make a few different ones, so I'm trying to get data from a category.

Here are four times 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 the part of the tutorial that has statistics

 @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; 

display 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 display.

I can easily get data like the following

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

and he will print the number of checks

Now I am trying to get category data. I am correcting the code to get 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 a display 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'm trying to get the category name

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

but it always gets null

I think there may be a problem that is in the data type?

There are three names, and I do not know how to print them all.

Sorry, my native language is not English, so I can’t explain the situation very well.

+4
source share
1 answer

In JSON, categories are an array, so the property you are trying to map to should be:

 @property (strong, nonatomic) NSArray *categories; 

Then you need to change the registration code to iterate over the categories:

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

Class categories have a misleading name, as this is not a collection object (as you seem to have originally intended).

+7
source

Source: https://habr.com/ru/post/1482749/


All Articles