RestKit objectModeling动态数据

时间:2012-04-30 18:38:55

标签: objective-c ios json rest restkit

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

2 个答案:

答案 0 :(得分:2)

是的,可以使用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];

}

完成上述所有操作后,您可以使用以下方法获取您的Thing List:

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

答案 1 :(得分:0)

您可以随时使用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没有直接关系,但这可能是一种帮助你的方法。