gpt4 book ai didi

ios - 如何将 RestKit 资源路径映射与查询字符串一起使用?

转载 作者:行者123 更新时间:2023-12-02 01:43:20 26 4
gpt4 key购买 nike

我正在尝试使用对象加载器从 API 中获取对象,该 API 需要传入类型作为查询参数(例如 http://example.com/api/households/:householdId/checklist_items?kind=todo ),但无论我尝试以哪种方式注册资源路径,它都不会似乎不想找到我的 map 。

我正在设置这样的映射:

RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RMChecklistItem class]];
[mapping mapKeyPath:@"id" toAttribute:@"checklistItemId"];
[mapping mapKeyPath:@"kind" toAttribute:@"kind"];
[mapping mapKeyPath:@"title" toAttribute:@"title"];
[mapping mapKeyPath:@"completed" toAttribute:@"completed"];
[mapping mapKeyPath:@"abilities" toAttribute:@"abilities"];
[mapping mapKeyPath:@"comments" toRelationship:@"comments"
withMapping:[provider objectMappingForClass:[RMComment class]]];

并使用基本路径和带有查询字符串的版本向对象映射提供程序注册它:

[provider addObjectMapping:mapping];
[provider setObjectMapping:mapping forResourcePathPattern:@"/api/households/:householdId/checklist_items"];
[provider setObjectMapping:mapping forResourcePathPattern:@"/api/households/:householdId/checklist_items?kind=:kind"];

但是当我尝试使用它时:

NSDictionary *params [NSDictionary dictionaryWithObject:@"todo" forKey:@"kind"];
NSString *path = [NSString stringWithFormat:@"/api/households/%i/checklist_items", householdId.intValue];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[path stringByAppendingQueryParameters:params] usingBlock:^(RKObjectLoader *loader) {
loader.onDidLoadObjects = ^(NSArray *objects) {
NSLog(@"%@", objects);
};
loader.onDidFailWithError = ^(NSError *error) {
NSLog(@"%@", error);
};
loader.onDidFailLoadWithError = ^(NSError *error) {
NSLog(@"%@", error);
};
}];

事情很糟糕:

2012-07-20 12:13:18.533 roommates[22399:13d03] D restkit.object_mapping:RKObjectMapper.m:336 Performing object mapping sourceObject: (
{
abilities = {
destroy = 1;
};
comments = (
);
completed = 0;
id = 44;
kind = todo;
title = "and do the dishes";
},
{
abilities = {
destroy = 1;
};
comments = (
{
body = "your!";
"created_at" = "2012-07-18T22:20:08-05:00";
"creator_id" = 16;
id = 70;
}
);
completed = 0;
id = 43;
kind = todo;
title = "pick you shit up";
},
{
abilities = {
destroy = 1;
};
comments = (
{
body = "did it";
"created_at" = "2012-07-18T22:19:56-05:00";
"creator_id" = 16;
id = 69;
}
);
completed = 0;
id = 40;
kind = todo;
title = "Pay Rent";
}
)
and targetObject: (null)
2012-07-20 12:13:18.539 roommates[22399:13d03] T restkit.object_mapping:RKObjectMapper.m:293 Examining keyPath 'users' for mappable content...
2012-07-20 12:13:18.539 roommates[22399:13d03] D restkit.object_mapping:RKObjectMapper.m:120 Found a collection containing only NSNull values, considering the collection unmappable...
2012-07-20 12:27:47.702 roommates[22399:13d03] D restkit.object_mapping:RKObjectMapper.m:303 Found unmappable value at keyPath: users
2012-07-20 12:27:47.705 roommates[22399:13d03] D restkit.object_mapping:RKObjectMapper.m:367 The following operations are in the queue: (
)
2012-07-20 12:27:51.065 roommates[22399:13d03] W restkit.object_mapping:RKObjectMapper.m:87 Adding mapping error: Could not find an object mapping for keyPath: ''
2012-07-20 12:27:51.066 roommates[22399:13d03] E restkit.network:RKObjectLoader.m:234 Encountered errors during mapping: Could not find an object mapping for keyPath: ''

最佳答案

因此,在调试器中观察流程时,我遇到了 RKObjectMappingProvider 的 mappingForPatternMatchingString:context: ,它调用 RKPathMatcher 的 matchesPath:string tokenizeQueryStrings:NO parsedArguments:nil 所以我猜这是通过设计。

我实际上只有查询参数“todo”和“shopping”的两个可能值,因此注册这两个值并不是不合理的。

更新:我提交了一个拉取请求,该请求更改了此设置以解析查询字符串中的 token :https://github.com/RestKit/RestKit/pull/871

关于ios - 如何将 RestKit 资源路径映射与查询字符串一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11583418/

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