这是我对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)
{
...
}];
我在应用程序中使用RestKit几次,也使用sid(登录令牌)。所以地址或方法调用没有问题。问题必须在映射中,但我不知道该怎么做。我试图将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];
答案 0 :(得分:1)
删除addAttributeMappingsFromDictionary:
的使用情况,并更改为:
[isFavoriteMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"Is_favorited"]];
然后将响应描述符上的keyPath更改为@"is_favorited"
。
这称为nil密钥路径映射(因为映射中的nil源键路径)。