【发布时间】:2014-04-09 03:48:19
【问题描述】:
这是我对 API 的回应:
{"is_favorited":1}
我想通过 RestKit 映射它,但我无法让它工作。这是我在 AppDelegate.m 中的:
RKObjectMapping *isFavoriteMapping = [RKObjectMapping mappingForClass:[IsFavorite class]];
[isFavoriteMapping addAttributeMappingsFromDictionary:@{
@"is_favorited" : @"Is_favorited",
}];
RKResponseDescriptor *isFavoriteResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:isFavoriteMapping
method:RKRequestMethodAny
pathPattern:@"/api/isPointFavorited/:sid"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[manager addResponseDescriptor:isFavoriteResponseDescriptor];
路径没问题。我在地址中有令牌。我是这样称呼它的:
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager getObjectsAtPath: [NSString stringWithFormat:@"/api/isPointFavorited/%@", sid]
parameters:@{
@"pid":[NSString stringWithFormat:@"%@", actualPlace.Id]
}
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
...
}
failure:^(RKObjectRequestOperation *operation, NSError *error)
{
...
}];
我在应用程序和 sid(登录令牌)中多次使用 RestKit。所以地址和方法调用都没有问题。问题一定出在映射中,但我不知道该怎么做。我尝试将 keyPath 从 nil 更改为 @"is_favorited" 但没有帮助。
我的 Is_favorite.h:
@interface IsFavorite : NSObject
@property(nonatomic) NSNumber *Is_favorited;
@end
RestKit 错误:
Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0x1849cbc0 {DetailedErrors=(
), NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: events, results
The representation inputted to the mapper was found to contain nested object representations at the following key paths: is_favorited
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}
编辑 - AppDelegate 发生更改但仍无法正常工作:
RKObjectMapping *isFavoriteMapping = [RKObjectMapping mappingForClass:[IsFavorite class]];
[isFavoriteMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"Is_favorited"]];
RKResponseDescriptor *isFavoriteResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:isFavoriteMapping
method:RKRequestMethodAny
pathPattern:@"/api/isPointFavorited/:sid"
keyPath:@"is_favorited"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[manager addResponseDescriptor:isFavoriteResponseDescriptor];
【问题讨论】:
标签: ios mapping restkit restkit-0.20