Objective-C RestKit 无输出
Objective-C RestKit no Output
我正在使用 RestKIT 为我想使用的 API 发出 GET 请求。可惜没有输出。这是 GET 请求的输出:
{
"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];
}
我制作了两个 classes,一个用于具有 NSNumber "products_count" 的结果,一个具有所有产品态度的 Product class。问题是即使建立了连接并且没有 "error",mappingResult.array 显然是空的。代码有什么错误?
您实际上并不需要 Result
class,因为阵列已经为您提供了帐户,您可以直接进入阵列。
因此,您最初的问题是您在响应描述符中使用了错误的映射。您只需要切换到结果映射即可。
您可以在响应描述符中使用产品映射,但您应该将关键路径更改为result.products
。
您的产品映射也有问题,因为 description
是所有 NSObject
子 class 上的现有方法,因此您需要通过添加 属性 称为 overview
或 title
.
我正在使用 RestKIT 为我想使用的 API 发出 GET 请求。可惜没有输出。这是 GET 请求的输出:
{
"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];
}
我制作了两个 classes,一个用于具有 NSNumber "products_count" 的结果,一个具有所有产品态度的 Product class。问题是即使建立了连接并且没有 "error",mappingResult.array 显然是空的。代码有什么错误?
您实际上并不需要 Result
class,因为阵列已经为您提供了帐户,您可以直接进入阵列。
因此,您最初的问题是您在响应描述符中使用了错误的映射。您只需要切换到结果映射即可。
您可以在响应描述符中使用产品映射,但您应该将关键路径更改为result.products
。
您的产品映射也有问题,因为 description
是所有 NSObject
子 class 上的现有方法,因此您需要通过添加 属性 称为 overview
或 title
.