我的問題取一個簡單的字符串JSON數組是如何使用restkit
RestKit: how to fetch a simple JSON array of strings?
但是重複,而原作者找到答案有幫助,我還是輸了。這個問題有兩個獨立的參考文獻,這兩個參考文獻都不足以幫助我(完整的初學者在KVC
和RestKit
)。
一個參考是here並給出了此示例代碼
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Whatever class]];
[mapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"someAttribute"]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:@"/whatever" keyPath:nil statusCodes:nil];
什麼是不管類應該是什麼?應該是NSString嗎?或者這是暗示我應該創建一個包裝類,它有一個屬性是NSArray *?
什麼是@「someAttribute」應該是?同樣,這是否暗示了一個包裝類,在這種情況下,someAttribute將是我的封裝類屬性的名稱,它是NSArray *?
另一個參考是here,雖然它顯示從數組中獲取「包裝類」,但包裝類只有一個NSNumber,而不是數組。所以目前還不清楚如何將這個例子轉化爲一個能夠獲得整個數組的函數。
作爲參考,服務器正在返回一個字符串數組,就像原始問題:[「string1」,「string2」,「string3」]。
UPDATE
這裏是代碼,我已經嘗試。我創建了一個包裝類。
@interface CannedResponse : NSObject
@property (strong, nonatomic) NSString * response;
@end
這裏是映射:
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[CannedResponse class]];
[mapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"response"]];
[manager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodGET pathPattern:RESPONSES_API_PATH keyPath:nil statusCodes:nil;
我打電話getObjectsAtPathForRouteName
什麼我得到了後背的預期CannedReponses
陣列。但不是將NSString
放入響應屬性中。我得到RKMappingSourceObject
。現在RKMappingSourceObject
確實包含正確的字符串。
我將通過登錄控制檯的RestKit
來查看我是否可以收集更多線索。
更新#2
如前所述here通過blakewatters我不這樣做在這個例子中的任何映射,可以只是繞過使用RestKit
。問題在於,我在服務層中編寫的所有代碼都使用RestKit
,並且我盲目地遵循所有調用的模式。對於這個調用,正確的答案可能是想知道如何直接訪問AFNetworking
。