gpt4 book ai didi

ios - RestKit 0.20 映射问题(使用来自 API 的 JSON)

转载 作者:行者123 更新时间:2023-11-29 03:03:30 25 4
gpt4 key购买 nike

我可以拉入springs name,但不能正确拉入leafs abbreviation

你能帮忙吗?将根据需要发布任何额外的代码。谢谢!

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

更新:为每个请求添加 header

地点.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

最佳答案

改变

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

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

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

然后改变这个

@property (nonatomic, strong) VenueVenue *venueVenue;

@property (nonatomic, strong) NSMutableArray *venueVenues;

对于您的 TableView ,我希望是这样的:

您应该有一系列 field 。部分的数量应该是场馆的计数

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

在委托(delegate)方法中,您使用 venue = self.venues[indexPath.section] 获取 field ,然后,对于行,venueVenue = venue.venueVenues[indexPath.row]

关于ios - RestKit 0.20 映射问题(使用来自 API 的 JSON),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23033119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com