Objective c RestKit对象建模动态数据
是否可以将键映射与以下JSON结构一起使用,还是必须手动执行值循环?举个例子就好了。他们的rest api并没有真正将真实世界的对象映射到标准对象结构中,只是按照XML属性建模(Objective c RestKit对象建模动态数据,objective-c,ios,json,rest,restkit,Objective C,Ios,Json,Rest,Restkit,是否可以将键映射与以下JSON结构一起使用,还是必须手动执行值循环?举个例子就好了。他们的rest api并没有真正将真实世界的对象映射到标准对象结构中,只是按照XML属性建模( 您可以始终使用Mac App Store中的Objectify($15 US)或JSON加速器($0.99 US)等工具来帮助生成此JSON的模型。然后,当您拥有阵列时,您可以使用类似NSArray的工具 -(void)makeObjectsPerformSelector:(SEL)aSelector 让所有对象查找一
您可以始终使用Mac App Store中的Objectify($15 US)或JSON加速器($0.99 US)等工具来帮助生成此JSON的模型。然后,当您拥有阵列时,您可以使用类似NSArray的工具
-(void)makeObjectsPerformSelector:(SEL)aSelector
让所有对象查找一个对象
就个人而言,我更喜欢使用块和
-(void)enumerateObjectsUsingBlock:(void(^)(id obj,insuinteger idx,BOOL*stop))块
循环遍历对象
它与RestKit没有直接关系,但这可能是一种帮助您解决问题的方法。是的,使用RestKit是可能的 你可以从官方那里获得阅读本文所需的所有文档,这里有你所需的解释。希望这对你有所帮助 首先,每个实体需要三个对象,一个用于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
#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
#import <Foundation/Foundation.h>
@interface SOThingList : NSObject{
NSDate *lastModifiedDate;
NSArray *myList;
}
@end
这将使您能够表示JSON的这一部分:
{"id": "","name": "Content Level","val": "Introductory"}
现在我们将定义如下:
SOThing.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
#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
#import <Foundation/Foundation.h>
@interface SOThingList : NSObject{
NSDate *lastModifiedDate;
NSArray *myList;
}
@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 SOAttribute : NSObject{
NSNumber *attributeId; //I use this nomenclature because the word 'id' is reserved, same for other objects.
NSString *name;
NSString *val;
}
@end
#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
#import <Foundation/Foundation.h>
@interface SOThingList : NSObject{
NSDate *lastModifiedDate;
NSArray *myList;
}
@end
和以前一样,我们需要一个列表,所以我们将myList定义为NSArray
现在,这很简单,这里有一个魔术,您可以向RestKit指示如何映射每个实体。
在您的应用程序代理上输入以下代码:
#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
@implementation SOAttribute
@end
#import "SOThing.h"
@implementation SOThing
@end
#import "SOThingList.h"
@implementation SOThingList
@end
- (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 :)
}];
您好,jgervin您希望事物只是一个元素,对吗?不同的事物在attributeList中有不同的条目吗?是的,attributeList是动态的,可以是任何东西。一次会这样,一个月后会有其他东西。感谢您提供非常详细的答案,但现在还没有。我不想要或需要SOAttribute obje我需要的是让Restkit在SOThing类中映射您在SOAttribute类中列出的项目。因此,将此[例如,{“id”:“name”:“Content Level”,“val”:“Introductive”}]转换为SOThing上的属性,如“Content Level”:“Introductive”我正在尝试去掉名称:blah val:blah。由于API发送了所有attributeList车库,我希望这些attributeList项转化为特定属性或特定属性数组。这些属性是定义的或任意的?我不太确定,但我想也许你可以使用动态对象映射实现你想要的。C你能发布一个你想要得到的对象的例子吗?我认为你不能使用RestKit“动态”修改SOThing对象,使其具有像ContentLevel这样的属性。另一方面,也许你可以建立一个字典,就像你提到的键:“Content Level”和值“Introduction”一样使用动态映射。但是我不知道当你有id时,你想如何映射属性。你能举个例子吗?这就是我开始这个问题的原因。今天他们有一个巨大的循环,循环通过每个任意对象(属性列表)并构建NSDictionary。我想要一个无缝映射,所以我没有NSDictionary。