gpt4 book ai didi

iphone - 核心数据中带有嵌套对象数组的 Restkit 复杂对象映射

转载 作者:太空狗 更新时间:2023-10-30 03:49:07 27 4
gpt4 key购买 nike

我正在使用 coredata 和 restkit 来映射数据。

这里是 getsales 调用的 json 响应

{   "success":true,
"sales" : [
{
"brands" :[
{"id":"637", "name":"XYZ"},
{"id":"638", "name":"abc"}
]
"end_date" = "2011-10-14 12:00:00",
"id" = 3794,
"image" = "http://dummy.something.com.jpg",
"name" = "test",
},
{
"brands" =[
{"id":"640", "name":"abc"}
],
"end_date" = "2011-10-14 12:00:00",
"id" = 3766,
"image" = "http://dummy.something.com.jpg",
"name" = "text2",
},
{
"brands" =[
{"id":"641", "name":"XYZ"},
{"id":"642", "name":"abc"},
{"id":"643", "name":"def"}
],
"end_date" = "2011-11-06 12:00:00",
"id" = 3798,
"image" = "http://dummy.something.com.jpg",
"name" = "test3",
}
]
}

我有

@interface Sale : NSManagedObject{   
}
@property (nonatomic, retain) NSNumber * ID;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSDate * endDate;
@property (nonatomic, retain) NSString * imageUrl;

@implementation Sale

@dynamic ID;
@dynamic name;
@dynamic startDate;
@dynamic endDate;
@dynamic imageUrl;

我正在尝试将响应映射如下

- (void)getSales{
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://baseurl.com"];
RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"base.sqlite"];
objectManager.objectStore = objectStore;


RKManagedObjectMapping* saleMapping = [RKManagedObjectMapping mappingForClass:[Sale class]];
[saleMapping mapKeyPathsToAttributes:
@"id", @"ID",
@"name", @"name",
@"start_date",@"startDate",
@"end_date", @"endDate",
@"image", @"imageUrl",
nil];

saleMapping.primaryKeyAttribute = @"ID";
[[RKObjectManager sharedManager].mappingProvider setMapping:saleMapping forKeyPath:@"sales"];

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/getSales/" objectMapping:saleMapping delegate:self];
}

基本上我有嵌套的对象数组,映射这些对象的正确方法是什么??Sale 类应该使用什么类型的属性来存储品牌列表??

感谢任何帮助,我已经浪费了太多时间来解决这个问题。

最佳答案

尝试添加:

[saleMapping mapKeyPath:@"brands"toRelationship:@"brands"withMapping:[BrandObject objectMapping]];

其中 [BrandObject objectMapping] 是 BrandObject 的映射(一个具有 idname 属性的 NSManagedObject)。

本质上,您可以使用 mapKeyPath:toRelationship:withMapping: 来嵌套映射。您还需要使用 NSSet 类型向 Sale 对象添加一个属性:

@property (nonatomic, retain) NSSet *brands;

最后,在 Sale 对象的实现中,添加:@dynamic brands 以及其他 @dynamic 语句。

希望对您有所帮助!谢谢。

关于iphone - 核心数据中带有嵌套对象数组的 Restkit 复杂对象映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7767788/

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