【问题标题】:Nested mapping using RKObjectMapping使用 RKObjectMapping 的嵌套映射
【发布时间】:2023-03-22 22:20:01
【问题描述】:

我正在尝试使用 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。但我收到警告说它已被弃用。我可以使用任何替代方案吗?

【问题讨论】:

    标签: iphone ios xcode restkit


    【解决方案1】:

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

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

    你应该给 searchRequestMapping like-

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

    除了 appendQueryParams:params,你可以使用 RKClient 的方法下面的方法-

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

    【讨论】:

    • 捂脸。对我来说真是愚蠢的错误。感谢您发现它拉胡尔。干杯!
    • 答案已过时
    • 嗨 Alberto,您能在此处更新/发布您的答案吗?几年前我已经停止在 iOS 上工作,所以我不知道最近的变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2012-04-30
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    相关资源
    最近更新 更多