RestKit 对象建模动态数据
Posted
技术标签:
【中文标题】RestKit 对象建模动态数据【英文标题】:RestKit objectModeling dynamic data 【发布时间】:2012-04-30 18:38:55 【问题描述】:是否可以使用具有以下 JSON 结构的键映射,或者我们是否必须手动执行值循环?例子会很棒。他们的 rest api 并没有真正将现实世界的对象映射到标准的对象结构中,而只是在 XML 属性之后建模。 :(
THINGS
"lastModifiedDate": "2012-02-23-08.43.16.916000",
"myList": [
"attributeList": [
"id": "","name": "Content Level","val": "Introductory",
"id": "","name": "Session Type","val": "Business Overview",
"id": "20110616053537016","name": "Speaker","val": "Jim Kim, Company1",
"id": "20110616053526559","name": "Speaker","val": "Bob Ironman, Company2",
"id": "20110803145027914","name": "Speaker","val": "Kristine Thomas, Company3",
"id": "","name": "Room","val": "Banyan",
"id": "","name": "Industry","val": "Cross Industry",
"id": "","name": "Loc","val": "Stadium I",
"id": "","name": "Topic Tag","val": "CMS Systems",
"id": "","name": "Status","val": "Accepted",
"id": "","name": "Sub-Event","val": "Leadership",
"id": "","name": "Session","val": "LVI",
"id": "","name": "SubTrack","val": "None",
"id": "","name": "Track","val": "Business Value Outsourcing"
],
"active": true,
"desc": "This is a really cool thing",
"end": "16:00",
"id": "2011080146112",
"num": "1002A",
"start": "15:00",
"title": "The thing title"
]
【问题讨论】:
嗨,jgervin,您希望 THINGS 只是一个元素,对吧? 不同的事物在attributeList中有不同的条目吗? 是的,attributeList 是动态的,可以是任何东西。一次又一个月后会是这样。 【参考方案1】:是的,RestKit 可以。
您可以从官方wiki 获得阅读本文所需的所有文档,在这里您可以了解如何做您需要做的事情。希望对您有所帮助!
首先,每个实体需要三个对象,一个用于 ThingsList,另一个用于表示一个事物,另一个用于表示一个 ThingAttribute。让我们从最基本的元素 ThingAttribute 开始:
SOAttribute.h
#import <Foundation/Foundation.h>
@interface SOAttribute : NSObject
NSNumber *attributeId; //I use this nomenclature because the word 'id' is reserved, same for other objects.
NSString *name;
NSString *val;
@end
SOAttribute.m
@implementation SOAttribute
@end
这将让您表示 JSON 的这一部分:
"id": "","name": "Content Level","val": "Introductory"
现在我们将 Thing 定义如下:
SOThing.h
#import <Foundation/Foundation.h>
@interface SOThing : NSObject
NSArray *attributeList; //This represent the list of attributes defined before
BOOL active;
NSString *desc;
NSString *title;
NSString *end;
NSString *start;
NSString *num;
NSNumber *thingId;
@end
SOThing.m
#import "SOThing.h"
@implementation SOThing
@end
如您所见,您必须将attributeList 定义为NSArray。 RestKit 会自动知道您需要在那里接收属性列表。所以我们定义了如下结构:
"attributeList": [...],
"active": true,
"desc": "This is a really cool thing",
"end": "16:00",
"id": "2011080146112",
"num": "1002A",
"start": "15:00",
"title": "The thing title"
最后你定义的事物列表如下:
SOThingList.h
#import <Foundation/Foundation.h>
@interface SOThingList : NSObject
NSDate *lastModifiedDate;
NSArray *myList;
@end
SoThingList.m
#import "SOThingList.h"
@implementation SOThingList
@end
和以前一样,我们需要一个事物列表,因此我们将 myList 定义为一个 NSArray。
现在,这很简单,接下来就是您向 RestKit 指示如何映射每个实体的神奇之处。 在您的 App Delegate 上输入以下代码:
#import "SOThingList.h"
#import "SOThing.h"
#import "SOAttribute.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
RKObjectMappingProvider *mappingProvider = [RKObjectManager sharedManager].mappingProvider;
RKObjectMapping *mappingForAttribute = [RKObjectMapping mappingForClass:[SOAttribute class]];
//This can be mapped directly
[mappingForAttribute mapAttributes:@"name",@"val", nil];
//here you indicate the special case, id->attributeId on our object
[mappingForAttribute mapKeyPath:@"id" toAttribute:@"attributeId"];
//Set the new mapping into the mapping provider.
[mappingProvider addObjectMapping:mappingForAttribute];
RKObjectMapping *mappingForThing = [RKObjectMapping mappingForClass:[SOThing class]];
//Same as before, these attributes can be mapped directly
[mappingForThing mapAttributes:@"active",@"title",@"end",@"start",@"desc",@"num", nil];
[mappingForThing mapKeyPath:@"id" toAttribute:@"thingId"];
//Here I indicate that any object on NSArray on attributeList should be mapped using SOAttribute mapping defined above.
[mappingForThing mapKeyPath:@"attributeList" toRelationship:@"attributeList" withMapping:mappingForAttribute];
[mappingProvider addObjectMapping:mappingForThing];
RKObjectMapping *mappingForThingsList = [RKObjectMapping mappingForClass:[SOThingList class]];
[mappingForThingsList mapAttributes:@"lastModifiedDate", nil];
[mappingForThingsList mapKeyPath:@"myList" toRelationship:@"myList" withMapping:mappingForThing];
[mappingProvider addObjectMapping:mappingForThingsList];
完成上述所有操作后,您可以通过以下方法使用块获取您的事物列表:
SomeObject.m
- (void) get: (NSString *) resourcePath onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock
RKObjectMapping *mapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[SOThingList class]];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader* loader)
loader.objectMapping = mapping;
loader.delegate = self;
loader.onDidLoadObject = loadBlock;
loader.onDidFailWithError = ^(NSError * error)
NSLog(@"%@",error);
;
loader.onDidFailLoadWithError = failBlock;
loader.onDidLoadResponse = ^(RKResponse *response)
//Do something
;
];
然后你就这样使用它......
[someObjectInstance get:@"http://mydomain.com/mywebservice/" onLoad:^(id object)
SOThingList *thingList = (SOThingList *) object;
//Do something with your shiny thingList
onError:^(NSError *error)
//Display an error message :)
];
【讨论】:
感谢您提供非常详细的答案,但还没有。我不想要或不需要 SOAttribute 对象。我需要的是 Restkit 在 SOThing 类中映射您在 SOAttribute 类中列出的项目。所以把这个[例如。 "id": "","name": "Content Level","val": "Introductory" ] 像这样 "Content Level":"Introductory" 我试图摆脱名称:blah val:blah。由于 API 发送了所有的属性列表车库,我希望这些属性列表项变成特定的属性或特定属性的数组。 这些属性是定义的还是任意的?我不太确定,但我想也许你可以使用动态对象映射来实现你想要的。您能否发布一个您想要获取的对象的示例? 我认为您将无法使用 RestKit“动态地”修改 SOThing 对象以具有 ContentLevel 等属性。另一方面,也许您可以使用 Key: "Content Level" 和 value "Introduction" 使用动态映射来构建字典。但是我不知道当你有一个 id 时你想如何映射属性......你能举个例子吗? 这就是我开始这个问题的原因。今天,他们有一个巨大的循环,可以遍历每个任意对象(attriblist)并构建 NSDictionaries。我想要一个无缝映射,所以我没有 NSDictionaries。【参考方案2】:您始终可以使用 Mac App Store 中的 Objectify(15 美元)或 JSON Accelerator(0.99 美元)等工具来帮助生成此 JSON 的模型。然后,当您拥有数组时,您可以使用类似 NSArray 的东西
- (void)makeObjectsPerformSelector:(SEL)aSelector
让所有对象寻找一个对象。
我个人更喜欢使用积木和
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block
循环遍历事物
它与 RestKit 没有直接关系,但这可能是一种帮助你的方法。
【讨论】:
以上是关于RestKit 对象建模动态数据的主要内容,如果未能解决你的问题,请参考以下文章
动态属性作为在 iOS 中使用 Restkit 发布 JSON 的关键
RestKit 的动态映射会解决这个复杂的 JSON 映射吗?