【问题标题】:RestKit 0.20 mapping trouble (using JSON from API)RestKit 0.20 映射问题(使用来自 API 的 JSON)
【发布时间】: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 ({[{[{) 才能到达“缩写”所在的位置。

标签: ios json api restkit


【解决方案1】:

改变

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

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

因为响应中与所需关系内容相等的数组内容位于“叶子”键下,而不是内部“缩写”键下。

然后改变这个

@property (nonatomic, strong) VenueVenue *venueVenue;

@property (nonatomic, strong) NSMutableArray *venueVenues;

对于您的表格视图,我希望是这样的:

您应该有一系列场地。部分的数量应该是场地的count

对于每个部分,您可以有一个标题来显示场地详细信息,行数是venues.venueVenuescount(尽管您确实应该重命名VenueVenue 类和属性名称)。

在委托方法中,您使用venue = self.venues[indexPath.section] 获得场地,然后对于行,venueVenue = venue.venueVenues[indexPath.row]

【讨论】:

  • 会改变。那么不要为此添加额外的 RKResponseDescriptor ,保持原样?只是想知道我是否需要添加RKResponseDescriptor *responseDescriptorLeafs = [RKResponseDescriptor responseDescriptorWithMapping:venuevenueMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"leafs" statusCodes:[NSIndexSet indexSetWithIndex:200]]; 或其他任何内容
  • 在我的RKObjectMapping *venuevenueMapping 中我是否需要使用addAttributeMappingsFromArray:@[@"abbreviation"]]; 而不是addAttributeMappingsFromDictionary:@{@"abbreviation": @"abbreviation"}];
  • 那个响应描述符永远不会匹配任何东西(你不能索引到 springs 数组中)。映射是相同的,数组方法只是简写形式。
  • 当我这样做时刚刚崩溃reason: '[<Venue 0xa2e3150> valueForUndefinedKey:]: this class is not key value coding-compliant for the key abbreviation.'有什么想法吗?
  • 不会崩溃,除非我有除leafs 以外的任何字眼,如果有帮助的话
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多