gpt4 book ai didi

objective-c - RestKit 对象建模动态数据

转载 作者:行者123 更新时间:2023-11-28 22:58:12 24 4
gpt4 key购买 nike

是否可以使用具有以下 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"
}
]
}

最佳答案

是的,使用 RestKit 是可能的。

您可以从官方wiki获取阅读本文所需的所有文档。在这里你有做你需要做的事情的解释。希望对您有所帮助!

首先,每个实体需要三个对象,一个用于 ThingsList,另一个用于表示一个 Thing,另一个用于表示一个 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];

}

完成上述所有操作后,您可以使用以下方法使用 block 获取您的事物列表:

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 :)
}];

关于objective-c - RestKit 对象建模动态数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10388806/

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