gpt4 book ai didi

ios - 在 RestKit 0.20 中映射对象时出错

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:17:55 29 4
gpt4 key购买 nike

我正在研究 Rest Kit 0.2o,我正在使用来自 Web 的 Json 格式的一些示例 URL,响应如下所示。我正在使用对象映射。我正在关注以下链接作为引用“https://github.com/RestKit/RestKit/wiki/Object-Mapping ” 我正在使用链接中给出的几乎相同的响应。

{
"geonames": [
{
"fcodeName":"capital of a political entity",
"toponymName":"Mexico City",
"countrycode":"MX",
"fcl":"P",
"fclName":"city, village,...",
"name":"Mexiko-Stadt",
"wikipedia":"en.wikipedia.org/wiki/Mexico_City",
"lng":-99.12766456604,
"fcode":"PPLC",
"geonameId":3530597,
"lat":19.428472427036,
"population":12294193
},
{
"fcodeName":"capital of a political entity",
"toponymName":"Manila",
"countrycode":"PH",
"fcl":"P",
"fclName":"city,village,...",
"name":"Manila",
"wikipedia":"en.wikipedia.org/wiki/Manila",
"lng":120.9822,
"fcode":"PPLC",
"geonameId":1701668,
"lat":14.6042,
"population":10444527
}]
}

下面是代码

RKObjectMapping *newsMapping = [RKObjectMapping mappingForClass:[News class]];
[newsMapping addAttributeMappingsFromDictionary:@{
@"fcodeName": @"fcodeName",
@"toponymName": @"toponymName",
@"countrycode": @"countrycode",
@"fcl": @"fcl",
@"fclName": @"fclName",
@"name":@"name",
@"wikipedia":@"wikipedia",
@"lng":@"lng",
@"fcode":@"fcode",
@"geonameId":@"geonameId",
@"lat":@"lat",
@"population":@"population",
}];

RKResponseDescriptor* responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:newsMapping
pathPattern:nil
keyPath:@"newsMapping"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

NSURL* url = [[NSURL alloc]initWithString:@"http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];

[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// RKLogError(@"Operation failed with error: %@", error);
NSLog(@"THIS IS TEST %@",error);
}];

[objectRequestOperation start];

"News"是我正在使用的类,KeyPath 我正在使用的是 "geonames"

@interface News : NSObject

@property(nonatomic,copy)NSString* fcodeName;
@property(nonatomic,copy)NSString* toponymName;
@property(nonatomic,copy)NSString* countrycode;
@property(nonatomic,copy)NSString* fcl;
@property(nonatomic,copy)NSString* fclName;
@property(nonatomic,copy)NSString* name;
@property(nonatomic,copy)NSString* wikipedia;
@property(nonatomic,copy)NSString* lng;
@property(nonatomic,copy)NSString* fcode;
@property(nonatomic,copy)NSString* geonameId;
@property(nonatomic,copy)NSString* lat;
@property(nonatomic,copy)NSString* population;

完成所有这些后,我遇到了以下错误

2013-05-30 12:06:14.376 TestApp[7140:11603] I restkit.network:RKObjectRequestOperation.m:174 GET 'http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo'
2013-05-30 12:06:15.477 TestApp[7140:12b07] E restkit.network:RKObjectRequestOperation.m:236 Test GET 'http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo' (200 OK / 0 objects) [request=1.0969s mapping=0.0000s total=1.1056s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0x7675f20 {DetailedErrors=(
), NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: newsMapping
The representation inputted to the mapper was found to contain nested object representations at the following key paths: status
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}
2013-05-30 12:06:15.477 TestApp[7140:14d03] blockk
2013-05-30 12:06:15.478 TestApp[7140:14d03] erorrrrrr next ifr
2013-05-30 12:06:15.478 TestApp[7140:14d03] erorrrrrr next ifjhkr
2013-05-30 12:06:15.478 TestApp[7140:11603] THIS IS TEST Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0x7675f20 {DetailedErrors=(
), NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: newsMapping
The representation inputted to the mapper was found to contain nested object representations at the following key paths: status
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}

实际上是错误的,它在最后一行中将 keyPath 显示为 null 但是在浏览器中我得到了上面提到的响应。

请帮忙

最佳答案

您的响应描述符键路径错误。更改为:

RKResponseDescriptor* responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:newsMapping pathPattern:nil keyPath:@"geonames" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

您当前看到的错误似乎是因为实际返回的 JSON 是:

{"status":{"message":"the deaily limit of 30000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application.","value":18}}

这显然与您的期望不符......

关于ios - 在 RestKit 0.20 中映射对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16829648/

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