RestKit 0.20 映射问题(使用 API 中的 JSON)

问题描述 投票:0回答:1

我可以拉入

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

似乎没有正确地将属性映射添加到缩写

更新:为每个请求添加标头

地点.h

#import "VenueVenue.h"

@interface Venue : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) VenueVenue *venueVenue;
@property (nonatomic, strong) NSArray *leagues;

@end

地点地点.h

@interface VenueVenue : NSObject

@property (nonatomic, strong) NSString *abbreviation;

@end
ios json objective-c restkit
1个回答
1
投票

改变

relationshipMappingFromKeyPath:@"abbreviation" toKeyPath:@"abbreviation"

relationshipMappingFromKeyPath:@"leafs" toKeyPath:@"venueVenues"

因为响应中相当于您想要的关系内容的数组内容位于“leafs”键下,而不是内部“abbreviation”键下。

改变这个

@property (nonatomic, strong) VenueVenue *venueVenue;

@property (nonatomic, strong) NSMutableArray *venueVenues;

对于您的表格视图,我期望类似的内容:

您应该有一系列场地。分段数应为场馆的

count

对于每个部分,您可以有一个标题来显示场地详细信息,行数是

count
venues.venueVenues
(尽管您确实应该重命名
VenueVenue
类和属性名称)。

在委托方法中,您可以使用

venue = self.venues[indexPath.section]
获取场地,然后对于行,
venueVenue = venue.venueVenues[indexPath.row]

© www.soinside.com 2019 - 2024. All rights reserved.