Restkit给出映射错误:

Posted

技术标签:

【中文标题】Restkit给出映射错误:【英文标题】:Restkit giving mapping error: 【发布时间】:2013-05-30 00:15:09 【问题描述】:

我正在尝试映射 Foursquare (FS) 调用的结果。当我使用 AFNetworking 时,它会按预期返回 FS 结果(JSON)。当我尝试使用 Restkit 调用时,无论是使用 RKEntityMapping 还是 RKObjectMapping,我都会收到以下错误。

RKMapperOperation.m:98 添加映射错误:找不到任何属性或关系映射的可映射值

有什么建议吗?我错过了什么吗?我知道我没有一些位置信息的关系映射设置,但我似乎无法让它映射***内容。

RESTKIT ================

// --- VENUE MAPPING ------------------------------

RKEntityMapping *venueMapping = [RKEntityMapping mappingForEntityForName:@"Venue"
                                                    inManagedObjectStore:objectManager.managedObjectStore];

[venueMapping addAttributeMappingsFromArray:@[@"name"] ];

[venueMapping addAttributeMappingsFromDictionary:@
 @"id": @"venueID"
 ];


[venueMapping setIdentificationAttributes:@[@"venueID"] ];



// Routes for Users
[self.foursquareManager.router.routeSet addRoute:[RKRoute routeWithClass:[Venue class]
                                                    pathPattern:@"v2/venues/search"
                                                         method:RKRequestMethodGET]];


// Register our mappings with the provider
RKResponseDescriptor *venueResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:venueMapping
                                                                                       pathPattern:@"v2/venues/search"
                                                                                           keyPath:nil
                                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

RKResponseDescriptor *venueDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:venueMapping
                                                                               pathPattern:@"v2/venues/search"
                                                                                   keyPath:nil
                                                                               statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


[self.foursquareManager addResponseDescriptor:venueResponseDescriptor];
[self.foursquareManager addResponseDescriptor:venueDescriptor];

[self.foursquareManager getObject:nil
                                      path:@"v2/venues/search"
                                parameters:@
                                             @"client_id" : kFourSquareClientID,
                                             @"client_secret" : kFourSquareClientSecret,
                                             @"ll" : searchString,
                                             @"v" : @"20130101"
                                            

                                   success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 

                                       DLog(@"Getting favorites return successful update tableview and CD ");


                                   
                                   failure:^(RKObjectRequestOperation *operation, NSError *error) 
                                       DLog(@"Getting all favorites failed: %@", [error localizedDescription]);

                                   ];

AFNETWORKING ==============

如果我使用 AF 进行调用,它会完美返回 JSON 数据。

NSString *searchString = [NSString stringWithFormat:@"%@,  %@",[dictionary objectForKey:@"lat"], [dictionary objectForKey:@"lng"]];
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"https://api.foursquare.com"]];


NSDictionary *parameters = @
                             @"client_id" : kFourSquareClientID,
                             @"client_secret" : kFourSquareClientSecret,
                             @"ll" : searchString,
                             @"v" : @"20130101"
                             ;

NSMutableURLRequest *request = [client requestWithMethod:@"GET" path:@"/v2/venues/search" parameters:parameters];
DLog(@"");
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) 


    NSError *error;
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:JSON //1
                          options:kNilOptions
                          error:&error];

    NSLog(@"THIS IS A DICTIONARY (or array): %@", JSON);

 failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) 
    NSLog(@"if requestFailed");
];

[operation start];

我的场地对象 ==========

@interface Venue : NSManagedObject

@property (nonatomic, retain) NSString * venueID;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * address;
@property (nonatomic, retain) NSNumber * verified;

@end

【问题讨论】:

【参考方案1】:

发现问题。这将解决它。

keyPath:@"response.venues"

FourSquare 返回的 JSON 有许多属性和信息子数组。 keyPath 是您需要经过的路径才能到达 JSON 数据的“地点”。如果您需要获取返回的“位置”信息,您将使用以下 keyPath(遍历层次结构)。

keyPath:@"response.venues.location"

【讨论】:

你能解释一下为什么它会修复它吗?

以上是关于Restkit给出映射错误:的主要内容,如果未能解决你的问题,请参考以下文章

Restkit 对象映射在映射过程中遇到错误

RestKit 映射错误的对象

Restkit 对象映射两个类错误访问

RestKit 映射错误“无法将对象集合映射到非可变集合。”

Restkit 属性映射失败的转换错误

RestKit 实体映射