0
我正在使用RestKIT爲我想要使用的API發出GET請求。可悲的是沒有輸出。這是GET請求的輸出:Objective-C RestKit no輸出
{
"return_code": 0,
"return_message": "",
"result": {
"products_count": 2,
"product": [
{
"id": "2440508995",
"name": "Long Sweater",
"description": "This product guarantees warmth in times of need.",
"price": 8,
"categories_ids": "73978179"
},
{
"id": "2455113539",
"name": "Tommy Hilfiger Pants",
"description": "Exclusive brown pants",
"price": 13,
"categories_ids": ""
}
]
}
}
很顯然,我只想得到一個數組,其中所有的產品都存儲。這是我的方法來做到這一點。
- (void)loadProducts
{
RKObjectMapping* resultMapping = [RKObjectMapping mappingForClass:[Result class]];
[resultMapping addAttributeMappingsFromDictionary:@{
@"products_count": @"products_count",
}];
RKObjectMapping* productMapping = [RKObjectMapping mappingForClass:[Product class]];
[productMapping addAttributeMappingsFromDictionary:@{
@"id": @"id",
@"name": @"name",
@"description": @"description",
@"price": @"price",
@"categories_ids": @"categories_ids"
}];
[resultMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"product" toKeyPath:@"product" withMapping:productMapping]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:productMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"result" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSURL *URL = [NSURL URLWithString:@"https://api.api2cart.com/v1.0/product.list.json?api_key=//some keyf&store_key=//some key"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load collection of Products: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];
}
我做了兩個班,一個是具有NSNumber的「products_count」,並與所有的產品態度,一個產品類的結果。問題是即使建立了連接並且沒有「錯誤」,mappingResult.array顯然是空的。代碼中的錯誤是什麼?
您正在結果響應描述符中使用產品映射... – Wain
所以我用resultMapping替代它? –
這將是第一件要做的事情。你真的需要結果容器嗎?您可以直接向下鑽取具有響應描述符鍵路徑的產品... – Wain