gpt4 book ai didi

iphone - 使用 RKObjectMapping 进行嵌套映射

转载 作者:行者123 更新时间:2023-11-29 04:19:00 24 4
gpt4 key购买 nike

我正在尝试使用 RKObjectMapping 执行对象映射。

这是我的 JSON 示例:

{
"notification": {
"created_at": "2012-10-23T02:43:43-04:00",
"delivered": true,
"id": 3915,
"missed": false,
"rejected": false,
"search_request_id": 360,
"bid": {
"bid_type": "none",
"bid_type_values": null,
"comment": "hi\n",
"created_at": "2012-10-23T02:43:54-04:00",
"delivered": true,
"delivery_type": "email",
"id": 268,
"has_chat_session": true,
},
"search_request": {
"created_at": "2012-10-23T02:43:42-04:00",
"customer_id": 6,
"id": 360,
"latitude": "40.7536854",
"location": "10001",
"longitude": "-73.9991637",
}
}

我已经使用 jsonlint 验证了 JSON,它是有效的。

这是我的映射。

    RKObjectMapping *bidMapping = [RKObjectMapping mappingForClass:[Bid class]]; 
[bidMapping mapKeyPath:@"comment" toAttribute:@"comment"];
[bidMapping mapKeyPath:@"chat_session_id" toAttribute:@"chatSessionId"];
[objectManager.mappingProvider setMapping:bidMapping forKeyPath:@"bid"];

RKObjectMapping *searchRequestMapping = [RKObjectMapping mappingForClass:[SearchRequest class]];
[searchRequestMapping mapKeyPath:@"purpose" toAttribute:@"purpose"];
[searchRequestMapping mapKeyPath:@"customer_Id" toAttribute:@"customerId"];
[searchRequestMapping mapKeyPath:@"need" toAttribute:@"need"];
[objectManager.mappingProvider setMapping:searchRequestMapping forKeyPath:@"search_request"];

RKObjectMapping *notificationMapping = [RKObjectMapping mappingForClass:[Notification class]]; // have not mapped all the attributes.
[notificationMapping mapKeyPath:@"id" toAttribute: @"notificationId"];
[notificationMapping mapKeyPath:@"created_at" toAttribute:@"timestamp"];
[notificationMapping mapKeyPath:@"vendor_id" toAttribute:@"vendorId"];
[notificationMapping mapKeyPath:@"bid" toRelationship:@"bid" withMapping:bidMapping];
[notificationMapping mapKeyPath:@"search_request" toRelationship:@"search_request" withMapping:notificationMapping];
[objectManager.mappingProvider setMapping:notificationMapping forKeyPath:@"notification"];

程序因断言失败而崩溃,当我尝试加载ObjectsAtResourcePath时,我无法调试该失败。我也尝试使用 rootKeyPaths 但没有成功。

有人可以帮我找出错误吗?

谢谢!

ps:我想添加一个 NSDictionary 参数,文档说使用appendQueryParams:params。但我收到一条警告,说它已被弃用。我可以使用任何替代方案吗?

最佳答案

您在这里给出了错误的映射-

    [notificationMapping mapKeyPath:@"search_request" toRelationship:@"search_request" withMapping:notificationMapping];

你应该给 searchRequestMapping 像 -

    [notificationMapping mapKeyPath:@"search_request" toRelationship:@"search_request" withMapping:searchRequestMapping];

您可以使用 RKClient 的方法以下方法来代替appendQueryParams:params-

- (RKRequest *)get:(NSString *)resourcePath queryParameters:(NSDictionary *)queryParameters delegate:(NSObject<RKRequestDelegate> *)delegate;

关于iphone - 使用 RKObjectMapping 进行嵌套映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13265152/

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