2012-11-06 78 views
0

我看到一些奇怪的行爲與RestKit和映射對象。在我的目標對象中,如果我有一個名爲'description'的NSString *屬性,那麼整個映射將失敗。將其更改爲任何其他名稱,或將其註釋掉,一切都會成功。無論是否爲該屬性設置了地圖,都會發生這種情況,並且我可以在簡單的測試類中重現該情況。RestKit對象映射失敗,具體取決於屬性名稱

的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>" 
    ); 
} 

是否有一個原因的映射只沒有這個屬性名稱?如果我將該名稱更改爲'description'以外的任何名稱,它也會成功。

回答

2

它與NSObject的描述屬性衝突。將它重命名爲其他(如desc)。

+0

謝謝,我不知道已經有一個描述屬性。 – mfanto