繁体   English   中英

使用Restkit 0.20进行映射,此类不符合密钥的密钥值编码要求

[英]Mapping with Restkit 0.20 this class is not key value coding-compliant for the key

您好,我在使用restkit从api映射数据时遇到问题。 我有下面的数据。 我真的很感谢任何可以指出我的错误的人。

{"GameId":1,"Items":

[{"Id":1,"PicUrl":"someurlhere","Answers":

["Paris","New York","San Francisco","Rome"],"CorrectAnswer":"San Francisco","Categories"}]}

这就是我计划在客户端存储数据的方式:

#import <Foundation/Foundation.h>

GameObjects.h

    @interface GameObjects : NSObject
    @property(nonatomic, strong)NSNumber *GameId;
    @property(nonatomic,strong)NSMutableArray *Items;
    @property(nonatomic,strong)NSNumber *IId;
    @property(nonatomic,strong)NSURL *PicUrl;
    @property(nonatomic,strong)NSMutableArray *Answers;
    @property(nonatomic,strong)NSString *CorrectAnswer;
    @property(nonatomic,strong)NSString *CategoryName;
    @end



GameObjects.m

#import "GameObjects.h"

@implementation GameObjects
@synthesize GameId;
@synthesize Items;
@synthesize Answers;
@synthesize IId;
@synthesize CategoryName;
@synthesize CorrectAnswer;
@synthesize PicUrl;
- (id)init
{
    self = [super init];
    if (self) {

        Items = [[NSMutableArray alloc]initWithObjects:PicUrl,Answers,CorrectAnswer,CategoryName, nil];

    }
    return self;
}

@end

这是我做映射的方法:

-(void)viewDidLoad
{

    //restkit
    RKObjectMapping *GameMapping = [RKObjectMapping mappingForClass:[GameObjects class]];

    [GameMapping addAttributeMappingsFromDictionary:@{   @"GameId":@"GameId",
                                                         @"Items": @"Items",
                                                         @"Id": @"Id",
                                                         @"PicUrl": @"PicUrl",
                                                         @"Answers":@"Answers",
                                                         @"CorrectAnswer":@"CorrectAnswer",
                                                         @"CategoryName":@"CategoryName"
                                                         }];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:GameMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"GameId" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    NSURL *URL = [NSURL URLWithString:@"someurlhere"];
    NSURLRequest *request =[NSURLRequest requestWithURL:URL];
    RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
    [objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        RKLogError(@"Operation failed with error: %@", error);
    }];
    [objectRequestOperation start];


}

和我的输出:

2013-09-30 14:28:04.204 FlickRest[3826:a0b] I restkit:RKLog.m:34 RestKit logging initialized...
2013-09-30 14:28:04.478 FlickRest[3826:a0b] ViewController - viewDidLoad
2013-09-30 14:28:12.158 FlickRest[3826:a0b] I restkit.network:RKObjectRequestOperation.m:180 GET 'someurlhere'

2013-09-30 14:28:18.243 FlickRest[3826:1a07] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFNumber 0x8e20580> valueForUndefinedKey:]: this class is not key value coding-compliant for the key GameId.'
*** First throw call stack:
(
    0   CoreFoundation                      0x020ca5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01e4d8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x0215a6a1 -[NSException raise] + 17
    3   Foundation                          0x00c95b0a -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 282
    4   Foundation                          0x00c02b61 _NSGetUsingKeyValueGetter + 81
    5   Foundation                          0x00c0219b -[NSObject(NSKeyValueCoding) valueForKey:] + 260
    6   Foundation                          0x00c21c9a -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 409
    7   FlickRest                           0x0007fc31 -[RKMappingSourceObject valueForKeyPath:] + 833
    8   FlickRest                           0x00085247 -[RKMappingOperation applyAttributeMappings:] + 1671
    9   FlickRest                           0x0008cc3e -[RKMappingOperation main] + 4062
    10  Foundation                          0x00ca9a69 -[__NSOperationInternal _start:] + 671
    11  Foundation                          0x00c26798 -[NSOperation start] + 83
    12  FlickRest                           0x0007a2c5 -[RKMapperOperation mapRepresentation:toObject:atKeyPath:usingMapping:metadata:] + 1957
    13  FlickRest                           0x00078b40 -[RKMapperOperation mapRepresentation:atKeyPath:usingMapping:] + 1904
    14  FlickRest                           0x0007b4bd -[RKMapperOperation mapRepresentationOrRepresentations:atKeyPath:usingMapping:] + 829
    15  FlickRest                           0x0007be12 -[RKMapperOperation mapSourceRepresentationWithMappingsDictionary:] + 2210
    16  FlickRest                           0x0007c7db -[RKMapperOperation main] + 1403
    17  Foundation                          0x00ca9a69 -[__NSOperationInternal _start:] + 671
    18  Foundation                          0x00c26798 -[NSOperation start] + 83
    19  FlickRest                           0x000ccb7a -[RKObjectResponseMapperOperation performMappingWithObject:error:] + 1354
    20  FlickRest                           0x000cabb3 -[RKResponseMapperOperation main] + 2371
    21  Foundation                          0x00ca9a69 -[__NSOperationInternal _start:] + 671
    22  Foundation                          0x00c26798 -[NSOperation start] + 83
    23  Foundation                          0x00cabd34 __NSOQSchedule_f + 62
    24  libdispatch.dylib                   0x028de4b0 _dispatch_client_callout + 14
    25  libdispatch.dylib                   0x028cc088 _dispatch_queue_drain + 450
    26  libdispatch.dylib                   0x028cbe85 _dispatch_queue_invoke + 126
    27  libdispatch.dylib                   0x028cce25 _dispatch_root_queue_drain + 83
    28  libdispatch.dylib                   0x028cd13d _dispatch_worker_thread2 + 39
    29  libsystem_c.dylib                   0x02bf6e72 _pthread_wqthread + 441
    30  libsystem_c.dylib                   0x02bdedaa start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException

您已经设置了响应描述符keyPath:@"GameId" ,但是它应该是keyPath:nil 通过将其设置为@"GameId" RestKit导航到JSON,并尝试将所有映射内容应用于为GameId键设置的NSNumber值。

“此类不符合键的键值编码”,当映射中定义的键值关系在某种程度上与JSON键值不匹配时,通常会发生此错误。 在您的情况下,将keyPath设置为'GameId'意味着您的JSON必须将根密钥设置为'GameId',而事实并非如此,并且会导致崩溃。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM