【发布时间】:2014-04-12 16:31:42
【问题描述】:
我可以拉入springs 名称,但无法正确拉入leafs 缩写。
你能帮忙吗?将根据需要发布任何额外的代码。谢谢!
API JSON
{
"springs": [{
"name": "daff",
"links": {
"api": {
"springs": {
"href": "http://api.xxx.com/v1/springs/daff"
},
}
},
"leafs": [{
"name": "raff",
"abbreviation": "rf",
"season": {
"year": 2014,
},
},
ViewController.m
@property (nonatomic, strong) NSArray *venues;
@property (nonatomic, strong) NSArray *venuesLeafs;
- (void)configureRestKit
{
// initialize AFNetworking HTTPClient
NSURL *baseURL = [NSURL URLWithString:@"https://api.xxx.com"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
// setup object mappings
RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[Venue class]];
[venueMapping addAttributeMappingsFromArray:@[@"name"]];
RKObjectMapping *venuevenueMapping = [RKObjectMapping mappingForClass:[VenueVenue class]];
[venuevenueMapping addAttributeMappingsFromDictionary:@{@"abbreviation": @"abbreviation"}];
[venueMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"abbreviation" toKeyPath:@"abbreviation" withMapping:venuevenueMapping]];
// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:venueMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:@"springs"
statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return venues.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
Venue *venue = [venues objectAtIndex:section];
return venue.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Venue *venue = [venues objectAtIndex:indexPath.section];
VenueVenue *venuevenue = [venuesLeafs objectAtIndex:indexPath.row];
cell.textLabel.text = venuevenue.abbreviation;
return cell;
}
似乎没有正确地将属性映射添加到abbreviation。
更新:为每个请求添加标头
Venue.h
#import "VenueVenue.h"
@interface Venue : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) VenueVenue *venueVenue;
@property (nonatomic, strong) NSArray *leagues;
@end
VenueVenue.h
@interface VenueVenue : NSObject
@property (nonatomic, strong) NSString *abbreviation;
@end
【问题讨论】:
-
您是否在“弹簧”内的数组内的对象/字典内寻找“叶子”? (我不知道 RestKit 是如何工作的,但我怀疑它是邪恶的。)
-
是的,这正是我要找的!
-
好吧,如果你使用 NSJSONSerialization 它就在那里 --
[@springs][0][@"leafs"][0][@"abbreviation"]。 (不要忽视“叶子”是一个包含对象/字典的数组。) -
你说我应该使用
addAttributeMappingsFromArray:@[@"abbreviation"]];而不是addAttributeMappingsFromDictionary:@{@"abbreviation": @"abbreviation"}];吗? -
我没有最迷糊的——我没有尝试破解你的代码。但是 JSON 是 object/array/object/array/object (
{[{[{) 才能到达“缩写”所在的位置。