RestKit 简单 JSON,错误的 pathPAttern?
RestKit simple JSON, bad pathPAttern?
JSON:
代码:
NSURL *baseURL = [NSURL URLWithString:@"http://www.krzysztofkurzawa.com"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor *rk = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:@"articles" keyPath:@"articles" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:rk];
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor *rk2 = [RKResponseDescriptor responseDescriptorWithMapping:mapping2 pathPattern:@"articles/:id" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:rk2];
[[RKObjectManager sharedManager] getObjectsAtPath:@"articles" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"%@", [mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];
[[RKObjectManager sharedManager] getObjectsAtPath:@"articles/1" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"%@", [mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];
Error: No mappable object representations were found at the key paths
searched.", keyPath=null, NSLocalizedDescription=No response
descriptors match the response loaded.}
我没有任何想法。当 pathPattern 设置为 nil
时,第一个映射有效。为什么必须是nil
?也许需要它来创建休息路径模式。
可能对你有帮助
NSString *pathNotation = @"/Notations/:notation_id";
RKRoute *notationRoute = [RKRoute routeWithClass:[Notation class] pathPattern:pathNotation method:RKRequestMethodGET];
[self.objectManager.router.routeSet addRoute:notationRoute];
RKResponseDescriptor *notationMappingDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:notationMapping method:RKRequestMethodGET pathPattern:pathNotation keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
和获取:
[[RKObjectManager sharedManager] getObject:nil path:path parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
//do something
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
//do something
}];
你的路径不正确,另外,当运行你的代码我不得不使用方法时:
+ (instancetype)responseDescriptorWithMapping:(RKMapping *)mapping
method:(RKRequestMethod)method
pathPattern:(NSString *)pathPattern
keyPath:(NSString *)keyPath
statusCodes:(NSIndexSet *)statusCodes
您正在使用的那个说它已被弃用。我猜你可能使用的是旧版本的 restkit。
这是有效的代码:
NSURL *baseURL = [NSURL URLWithString:@"http://www.krzysztofkurzawa.com"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor *rk = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodGET pathPattern:@"/articles/" keyPath:@"articles" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:rk];
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping2 addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor *rk2 = [RKResponseDescriptor responseDescriptorWithMapping:mapping2 method:RKRequestMethodGET pathPattern:@"/articles/:id/" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:rk2];
[[RKObjectManager sharedManager] getObjectsAtPath:@"/articles/" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"%@", [mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];
[[RKObjectManager sharedManager] getObjectsAtPath:@"/articles/1/" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"%@", [mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];
JSON:
代码:
NSURL *baseURL = [NSURL URLWithString:@"http://www.krzysztofkurzawa.com"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor *rk = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:@"articles" keyPath:@"articles" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:rk];
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor *rk2 = [RKResponseDescriptor responseDescriptorWithMapping:mapping2 pathPattern:@"articles/:id" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:rk2];
[[RKObjectManager sharedManager] getObjectsAtPath:@"articles" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"%@", [mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];
[[RKObjectManager sharedManager] getObjectsAtPath:@"articles/1" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"%@", [mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];
Error: No mappable object representations were found at the key paths searched.", keyPath=null, NSLocalizedDescription=No response descriptors match the response loaded.}
我没有任何想法。当 pathPattern 设置为 nil
时,第一个映射有效。为什么必须是nil
?也许需要它来创建休息路径模式。
可能对你有帮助
NSString *pathNotation = @"/Notations/:notation_id";
RKRoute *notationRoute = [RKRoute routeWithClass:[Notation class] pathPattern:pathNotation method:RKRequestMethodGET];
[self.objectManager.router.routeSet addRoute:notationRoute];
RKResponseDescriptor *notationMappingDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:notationMapping method:RKRequestMethodGET pathPattern:pathNotation keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
和获取:
[[RKObjectManager sharedManager] getObject:nil path:path parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
//do something
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
//do something
}];
你的路径不正确,另外,当运行你的代码我不得不使用方法时:
+ (instancetype)responseDescriptorWithMapping:(RKMapping *)mapping
method:(RKRequestMethod)method
pathPattern:(NSString *)pathPattern
keyPath:(NSString *)keyPath
statusCodes:(NSIndexSet *)statusCodes
您正在使用的那个说它已被弃用。我猜你可能使用的是旧版本的 restkit。
这是有效的代码:
NSURL *baseURL = [NSURL URLWithString:@"http://www.krzysztofkurzawa.com"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor *rk = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodGET pathPattern:@"/articles/" keyPath:@"articles" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:rk];
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping2 addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor *rk2 = [RKResponseDescriptor responseDescriptorWithMapping:mapping2 method:RKRequestMethodGET pathPattern:@"/articles/:id/" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:rk2];
[[RKObjectManager sharedManager] getObjectsAtPath:@"/articles/" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"%@", [mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];
[[RKObjectManager sharedManager] getObjectsAtPath:@"/articles/1/" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"%@", [mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];