2014-04-17 63 views
4

爲簡單起見,Rest API發送這個JSON response-body = {「status」:「ok」}。Restkit 0.22.0如何映射一個沒有keyPath的簡單JSON響應來發布

我建立我的Restkit映射像...創建了一個名爲StatusResponse類具有一個@property (nonatomic, assign) NSString *status;

RKObjectMapping *statusResponseMapping = [RKObjectMapping mappingForClass:[StatusResponse class]]; 
    [statusResponseMapping addAttributeMappingsFromDictionary:@{@"status":@"status"}]; 

// also tried : [statusResponseMapping addAttributeMappingsFromDictionary:@[@"status"]]; which resulted in same error 

    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); 

RKResponseDescriptor *statusResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:statusResponseMapping method:RKRequestMethodPOST pathPattern:@"status" keyPath:nil statusCodes:statusCodes]; 

[[RKObjectManager sharedManager] addResponseDescriptor:statusResponseDescriptor]; 

I'm running a post which is successful, but I get this error back: 

NSUnderlyingError=0x17024d440 "No mappable object representations were found at the key paths searched.", keyPath=null, NSLocalizedDescription=No response descriptors match the response loaded.} 
response.body={ 
    "status": "ok" 
} 

任何幫助,將不勝感激。

回答

3

你應該嘗試keyPath到@「」和pathPattern爲零,這裏是我正在使用的一段代碼,它對我來說完全正常工作。我正在做你完全一樣的嘗試。

RKResponseDescriptor *statusResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[ResponseStatus rkObjectMappingForResponse:YES] method:RKRequestMethodAny pathPattern:nil keyPath:@"" statusCodes:statusCodes]; 
+0

延遲應答道歉。我會嘗試你的建議。感謝分享。我會讓你知道我的結果。 – user3337849

+0

謝謝。你的代碼確實有效。再次感謝... – user3337849

0

因爲您的響應描述符有pathPattern:@"status",所以只有在向服務器上的status路徑發出請求時纔會使用映射。這可能不是你想要的。您可能應該使用pathPattern:nil

+0

我試圖用pathPattern:無成功。我需要知道如何爲沒有keyPath的簡單JSON對象{「status」:「ok」}創建ObjectMapping。當我簡單地嘗試使用keyPath:@「status」時,我得到錯誤鍵值編碼違規。我一直在閱讀ObjectMapping上的RestKit git wiki,但他們沒有展示如何爲這個簡單的情況。你有沒有可以指點我的參考? – user3337849