【问题标题】:iOS RestKit - mapping results, just one attributeiOS RestKit - 映射结果,只有一个属性
【发布时间】: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


    【解决方案1】:

    删除你对addAttributeMappingsFromDictionary:的使用,改为:

    [isFavoriteMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"Is_favorited"]];
    

    然后将响应描述符上的 keyPath 更改为 @"is_favorited"

    这称为 nil 键路径映射(因为映射中的源键路径为 nil)。

    【讨论】:

    • JSON 显示了整个 JSON?错误消息引用events, results...
    • 是的,引用是我的 AppDelegate 中的其他映射和响应描述符(这些在我设置 keyPath 的地方很少)。
    • 您应该在更改后出现不同的错误,这是什么?
    • 不,不同的UserInfo是一样的。
    • 如果您更改了密钥路径,那么您应该有不同的日志。打开映射的跟踪日志记录。您可以将日志粘贴到粘贴箱/gist 中吗?
    猜你喜欢
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多