我看到RestKit和映射对象的一些奇怪行为。在我的目标对象中,如果我有一个名为'description'的NSString *属性,那么整个映射都会失败。将此更改为任何其他名称,或将其注释掉,一切都会成功。无论我是否为该属性设置了地图,都会发生这种情况,我可以在简单的测试类中重现它。
服务器返回的JSON:
{
id = 1;
name = item1;
},
{
id = 2;
name = item2;
}
我要映射到的对象,SimpleObject.h。请注意,描述和非限制属性都不在JSON中,这意味着我不认为这是因为缺少“描述”:
#import <Foundation/Foundation.h>
@interface SimpleObject : NSObject
@property (nonatomic, assign) NSInteger identifier;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSString *nonexistant;
@end
SimpleObject.m:
#import "SimpleObject.h"
@implementation SimpleObject
@synthesize identifier;
@synthesize name;
@synthesize description;
@synthesize nonexistant;
@end
我在AppDelegate中创建了映射:
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURLString:@"http://127.0.0.1/test"];
// Setup our object mappings
RKObjectMapping *testMapping = [RKObjectMapping mappingForClass:[SimpleObject class]];
[testMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[testMapping mapKeyPath:@"name" toAttribute:@"name"];
// Register our mappings with the provider using a resource path pattern
[objectManager.mappingProvider setObjectMapping:testMapping forResourcePathPattern:@"/products"];
在我的didLoadObjects方法中,我有:
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
NSLog(@"Loaded products: %@", objects);
SimpleObject *obj = [objects objectAtIndex:0];
NSLog(@"Loaded product ID %d -> Name: %@ ", obj.identifier, obj.name);
}
输出:
Finished performing object mapping. Results: {
"" = (
(null),
(null)
);
}
如果我注释掉了描述属性(但是留在了不存在的地方),那么一切正常:
Finished performing object mapping. Results: {
"" = (
"<SimpleObject: 0x8a4d040>",
"<SimpleObject: 0x8a50130>"
);
}
是否有原因导致映射仅针对此属性名称失败?如果我将名称更改为“描述”以外的任何名称,它也会成功。
答案 0 :(得分:2)
与NSObject的描述属性冲突。将其重命名为其他(如desc)。