是的,使用 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
SO属性.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];
}
综上所述,您可以通过以下方法使用块获取您的事物列表:
一些对象.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 :)
}];