我看到 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
简单对象.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>"
);
}
映射仅针对此属性名称失败是否有原因?如果我将名称更改为“描述”以外的任何名称,它也会成功。