RestKit模型对象引发-valueForUndefinedKey:用于合成属性

时间:2013-03-13 20:40:56

标签: objective-c cocoa-touch cocoa restkit

我正在尝试映射到一个对象,但一直在

[<ItemObject 0x100353710> valueForUndefinedKey:]: this class is not key value coding-compliant for the key uid.

ItemObject.h

@interface ItemObject : NSObject

@property (nonatomic, retain) NSNumber * uid;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * itemType;

@end

ItemObject.m

@implementation ItemObject

@dynamic uid;
@dynamic title;
@dynamic itemType;

@end

方式

+ (void)searchFeatureById:(NSString *)searchId
{
    NSLog(@"Search Feature By ID: %@", searchId);
    RKObjectMapping *itemMapping = [RKObjectMapping mappingForClass:[ItemObject class]];

    [itemMapping addAttributeMappingsFromDictionary:@{
     @"id": @"uid",
        @"name": @"title",
        @"item_type": @"itemType"
     }];


    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:itemMapping pathPattern:nil keyPath:@"data" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
    // The entire value at the source key path containing the errors maps to the message
    [errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"errorMessage"]];
    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError);
    // Any response in the 4xx status code range with an "errors" key path uses this mapping
    RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping pathPattern:nil keyPath:@"error_description" statusCodes:statusCodes];
    RKObjectManager *manager = [RKObjectManager sharedManager];
    NSLog(@"HTTP Client: %@", manager.HTTPClient);

    [manager addResponseDescriptorsFromArray:@[ responseDescriptor, errorDescriptor ]];
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"false", @"with_lock"
                            , nil];
    NSString *path = [NSString stringWithFormat:@"/api/v1/features/%@", searchId];
    NSLog(@"Manager: %@", manager);
    [manager getObjectsAtPath:path parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
        NSLog(@"Results: %@", result);
        // Handled with articleDescriptor
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        // Transport error or server error handled by errorDescriptor
        NSLog(@"Error: %@", [error localizedDescription]);
        NSAlert *alert = [NSAlert alertWithMessageText:@"Error" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@", [error localizedDescription]];
        [alert runModal];

    }];
}

JSON响应

{
    "data": {
        "id": 1961,
        "item_type": "features",
        "assigned_to": {
        "id": 106
    },
    "parent": {
        "id": 0
    },
        "priority": {
        "id": 3
    },
        "project": {
        "id": 62
    },
        "release": {
        "id": 180
    },
        "reported_by": {
        "id": 106
    },
        "reported_by_customer_contact": {
        "id": 0
    },
        "status": {
        "id": 6
    },
        "workflow_step": {
        "id": 10
    },
        "actual_duration": {
        "duration": 0,
        "time_unit": {
            "id": 2
        }
    },
        "estimated_duration": {
        "duration": 8,
        "time_unit": {
            "id": 2
        }
    },
        "remaining_duration": {
        "duration": 0,
        "time_unit": {
            "id": 2
        }
    },
        "percent_complete": 0,
        "archived": false,
        "publicly_viewable": false,
        "completion_date": "1899-01-01T07:00:00Z",
        "due_date": "1899-01-01T07:00:00Z",
        "start_date": "2013-03-11T07:00:00Z",
        "description": "Create Login GUI",
        "name": "Login View",
        "notes": "",
        "number": "1961",
        "build_number": "",
        "custom_fields": {
            "custom_157": true,
            "custom_180": "",
            "custom_174": "",
            "custom_181": false,
            "custom_179": "",
            "custom_182": "",
            "custom_186": false,
            "custom_156": 0,
            "custom_161": "",
            "custom_163": "",
            "custom_164": "",
            "custom_165": "",
            "custom_167": "",
            "custom_185": []
        }
    }
}

崩溃

(
    0   CoreFoundation                      0x00007fff8d39bf56 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff8a7b2d5e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8d4261b9 -[NSException raise] + 9
    3   Foundation                          0x00007fff9716c703 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 240
    4   Foundation                          0x00007fff970a338e _NSGetUsingKeyValueGetter + 108
    5   Foundation                          0x00007fff970a3315 -[NSObject(NSKeyValueCoding) valueForKey:] + 392
    6   Foundation                          0x00007fff970c2da2 -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 348
    7   RestKit                             0x0000000100079675 -[RKMappingOperation shouldSetValue:atKeyPath:] + 85
    8   RestKit                             0x000000010007ad9f -[RKMappingOperation applyAttributeMapping:withValue:] + 2079
    9   RestKit                             0x000000010007c028 -[RKMappingOperation applyAttributeMappings:] + 1752
    10  RestKit                             0x00000001000832e0 -[RKMappingOperation main] + 3200
    11  Foundation                          0x00007fff970c56b4 -[__NSOperationInternal start] + 705
    12  RestKit                             0x000000010006c84e -[RKMapperOperation mapRepresentation:toObject:atKeyPath:usingMapping:metadata:] + 1870
    13  RestKit                             0x000000010006afbb -[RKMapperOperation mapRepresentation:atKeyPath:usingMapping:] + 1883
    14  RestKit                             0x000000010006da20 -[RKMapperOperation mapRepresentationOrRepresentations:atKeyPath:usingMapping:] + 832
    15  RestKit                             0x000000010006e26e -[RKMapperOperation mapSourceRepresentationWithMappingsDictionary:] + 1966
    16  RestKit                             0x000000010006ebbf -[RKMapperOperation main] + 1375
    17  Foundation                          0x00007fff970c56b4 -[__NSOperationInternal start] + 705
    18  RestKit                             0x00000001000e8f1c -[RKObjectResponseMapperOperation performMappingWithObject:error:] + 1116
    19  RestKit                             0x00000001000e7347 -[RKResponseMapperOperation main] + 2055
    20  Foundation                          0x00007fff970c56b4 -[__NSOperationInternal start] + 705
    21  Foundation                          0x00007fff970d8912 ____NSOQSchedule_block_invoke_2 + 124
    22  libdispatch.dylib                   0x00007fff8abe1a82 _dispatch_call_block_and_release + 18
    23  libdispatch.dylib                   0x00007fff8abe2961 _dispatch_worker_thread2 + 255
    24  libsystem_c.dylib                   0x00007fff9054c3da _pthread_wqthread + 316
    25  libsystem_c.dylib                   0x00007fff9054db85 start_wqthread + 13

使用RestKit 0.20.0-rc1进行Mac OS X开发

我做错了什么?如果我添加/删除对象属性,它会为新参数抛出相同的键/值错误。这让我相信它不是我的对象,但也许是关键路径?

更新

它跳转到RKMappingOperation.m文件行356。

- (BOOL)shouldSetValue:(id *)value atKeyPath:(NSString *)keyPath
{
    id currentValue = [self.destinationObject valueForKeyPath:keyPath]; //<-- this line here
    if (currentValue == [NSNull null]) {
        currentValue = nil;
    }

每个@noa更新2

2013-03-14 10:11:51.012 Project[55798:6a03] Destination Class: ItemObject
2013-03-14 10:11:51.012 Project[55798:6a03] Destination Class Super: NSObject

添加

[self.destinationObject objectForKey:@"uid"];

日志

2013-03-14 10:16:08.030 Project[55836:2203] -[ItemObject objectForKey:]: unrecognized selector sent to instance 0x103437e80

1 个答案:

答案 0 :(得分:3)

您在实现中使用的@dynamic指令告诉编译器生成该属性的方法。由于这些方法不存在,因此您的类与这些键不符合键值。 (即RestKit试图调用uid方法,但它不存在。)

你应该:

  • 删除@dynamic声明(以便编译器将合成所需的方法)或
  • 添加您自己的uidsetUid:
  • 的实施方案