RestKit映射成功后如何访问对象

Posted

技术标签:

【中文标题】RestKit映射成功后如何访问对象【英文标题】:How to access object after successful RestKit mapping 【发布时间】:2012-07-30 21:09:30 【问题描述】:

在关注RestKit documentation here 之后,我现在已经成功进行了对象映射。哇!我现在的问题是如何在整个项目中访问这个对象?我使用 RestKit 将 JSON 响应映射到 APIUser 对象。例如,在下一个屏幕上,我想从 APIUser 对象中获取用户的名字来打招呼。如何访问该对象?

我是 Objective-c 的新手,所以这可能更多是关于使用全局对象单例类跨类持久化数据的问题 在 Objective-C 中 比关于 RestKit 框架。

这里发布的是我的 restkit 对象映射日志,以防它有助于引用它。谢谢。任何帮助表示赞赏。

2012-07-30 14:54:04.813 TestApp[2361:1a707] D restkit.object_mapping:RKObjectMapper.m:240 Performing object mapping sourceObject: 
    apiUser =     
        active = 1;
        apiUserIdx = 101;
        birthdate = "1980-06-21T00:00:00-06:00";
        countryCode = US;
        firstName = "test_user1_firstname";
        lastName = "test_user1_lastname";
        ssn = 1111;
        userName = "test_user1";
        verified = 1;
    ;

 and targetObject: (null)
2012-07-30 14:54:04.813 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMapper.m:254 Examining keyPath 'errors' for mappable content...
2012-07-30 14:54:04.813 TestApp[2361:1a707] D restkit.object_mapping:RKObjectMapper.m:264 Found unmappable value at keyPath: errors
2012-07-30 14:54:04.814 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMapper.m:254 Examining keyPath 'apiUser' for mappable content...
2012-07-30 14:54:04.814 TestApp[2361:1a707] D restkit.object_mapping:RKObjectMapper.m:283 Found mappable data at keyPath 'apiUser': 
    active = 1;
    apiUserIdx = 101;
    birthdate = "1980-06-21T00:00:00-06:00";
    countryCode = US;
    firstName = "test_user1_firstname";
    lastName = "test_user1_lastname";
    ssn = 1111;
    userName = "test_user1";
    verified = 1;

2012-07-30 14:54:04.814 TestApp[2361:1a707] D restkit.object_mapping:RKObjectMapper.m:189 Asked to map source object 
    active = 1;
    apiUserIdx = 101;
    birthdate = "1980-06-21T00:00:00-06:00";
    countryCode = US;
    firstName = "test_user1_firstname";
    lastName = "test_user1_lastname";
    ssn = 1111;
    userName = "test_user1";
    verified = 1;
 with mapping RKObjectMapping class => APIUser: keyPath mappings => (
    "RKObjectKeyPathMapping: lastName => lastName",
    "RKObjectKeyPathMapping: birthdate => birthdate",
    "RKObjectKeyPathMapping: countryCode => countryCode",
    "RKObjectKeyPathMapping: firstName => firstName",
    "RKObjectKeyPathMapping: password => password",
    "RKObjectKeyPathMapping: apiUserIdx => apiUserIdx",
    "RKObjectKeyPathMapping: active => active",
    "RKObjectKeyPathMapping: userName => userName",
    "RKObjectKeyPathMapping: verified => verified"
)
2012-07-30 14:54:04.815 TestApp[2361:1a707] D restkit.object_mapping:RKObjectMappingOperation.m:428 Starting mapping operation...
2012-07-30 14:54:04.816 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:242 Mapping attribute value keyPath 'lastName' to 'lastName'
2012-07-30 14:54:04.816 TestApp[2361:1a707] D restkit.object_mapping:RKObjectPropertyInspector.m:95 Cached property names and types for Class 'APIUser': 
    accessibilityHint = NSString;
    accessibilityLabel = NSString;
    accessibilityLanguage = NSString;
    accessibilityValue = NSString;
    birthdate = NSDate;
    countryCode = NSString;
    firstName = NSString;
    lastName = NSString;
    password = NSString;
    userName = NSString;

2012-07-30 14:54:04.818 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:252 Mapped attribute value from keyPath 'lastName' to 'lastName'. Value: test_user1_lastname
2012-07-30 14:54:04.818 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:242 Mapping attribute value keyPath 'birthdate' to 'birthdate'
2012-07-30 14:54:04.818 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:115 Found transformable value at keyPath 'birthdate'. Transforming from type '__NSCFString' to 'NSDate'
2012-07-30 14:54:04.818 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:97 Transforming string value '1980-06-21T00:00:00-06:00' to NSDate...
2012-07-30 14:54:04.819 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:259 Skipped mapping of attribute value from keyPath 'birthdate to keyPath 'birthdate' -- value is unchanged ((null))
2012-07-30 14:54:04.821 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:242 Mapping attribute value keyPath 'countryCode' to 'countryCode'
2012-07-30 14:54:04.822 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:252 Mapped attribute value from keyPath 'countryCode' to 'countryCode'. Value: US
2012-07-30 14:54:04.822 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:242 Mapping attribute value keyPath 'firstName' to 'firstName'
2012-07-30 14:54:04.822 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:252 Mapped attribute value from keyPath 'firstName' to 'firstName'. Value: test_user1_firstname
2012-07-30 14:54:04.822 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:291 Did not find mappable attribute value keyPath 'password'
2012-07-30 14:54:04.823 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:242 Mapping attribute value keyPath 'apiUserIdx' to 'apiUserIdx'
2012-07-30 14:54:04.839 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:252 Mapped attribute value from keyPath 'apiUserIdx' to 'apiUserIdx'. Value: 101
2012-07-30 14:54:04.840 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:242 Mapping attribute value keyPath 'active' to 'active'
2012-07-30 14:54:04.840 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:252 Mapped attribute value from keyPath 'active' to 'active'. Value: 1
2012-07-30 14:54:04.840 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:242 Mapping attribute value keyPath 'userName' to 'userName'
2012-07-30 14:54:04.840 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:252 Mapped attribute value from keyPath 'userName' to 'userName'. Value: test_user1
2012-07-30 14:54:04.841 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:242 Mapping attribute value keyPath 'verified' to 'verified'
2012-07-30 14:54:04.845 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMappingOperation.m:252 Mapped attribute value from keyPath 'verified' to 'verified'. Value: 1
2012-07-30 14:54:04.845 TestApp[2361:1a707] D restkit.object_mapping:RKObjectMappingOperation.m:434 Finished mapping operation successfully...
2012-07-30 14:54:04.846 TestApp[2361:1a707] T restkit.object_mapping:RKObjectMapper.m:254 Examining keyPath 'error' for mappable content...
2012-07-30 14:54:04.846 TestApp[2361:1a707] D restkit.object_mapping:RKObjectMapper.m:264 Found unmappable value at keyPath: error
2012-07-30 14:54:04.846 TestApp[2361:1a707] D restkit.object_mapping:RKObjectMapper.m:304 Finished performing object mapping. Results: 
    apiUser = "<APIUser: 0xcd8b170>";

【问题讨论】:

【参考方案1】:

我想我明白了。我需要创建一个新的 APIUser 实例并将其设置为等于[objects objectAtIndex:0]. 如:

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects 

    NSLog(@"There are %i objects.", objects.count);

    APIUser *apiUser = [objects objectAtIndex:0];
    NSLog(@"Loaded apiUserIdx #%i -> First name: %@, Last name: %@", apiUser.apiUserIdx, apiUser.firstName, apiUser.lastName);


【讨论】:

以上是关于RestKit映射成功后如何访问对象的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 RestKit 映射根词典?

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

如何使用 RestKit 测试这个对象映射

RestKit:将单个对象映射到现有数组

RestKit 与特定核心数据对象的关系连接,无需键映射

RestKit .22:无法成功映射与嵌套 JSON 的关系