ios - RestKit:如何在没有任何嵌套属性的情况下将 NSManagedObject 作为 JSON 发布?

标签 ios iphone objective-c restkit restkit-0.20

这应该是一个非常简单的问题,但遗憾的是我没有找到任何答案......

RestKit 将我的对象映射到什么:

request.body={"user":{"pass":"1234","id":0,"login":"awesome_guy","tier":0}}

我真正想要的:

{"pass":"1234","id":0,"login":"awesome_guy","tier":0}

只是没有对象名称“user”。

如果您处理过此问题,您将需要 5 秒钟的时间来回答。如果你还没有使用过RestKit。你不知道答案。无论如何,我都会附上我的代码:

用户对象映射:

/* ===========================
 * ====User Object Mapping====
 * ==========================*/

RKEntityMapping* userObjectMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([User class]) inManagedObjectStore:objectManager.managedObjectStore];

NSDictionary *userObjectMappingDict = @{
                                    @"id":@"id",
                                    @"login":@"login",
                                    @"firstName":@"firstName",
                                    @"lastName":@"lastName",
                                    @"phoneNumber":@"phoneNumber",
                                    @"email":@"email",
                                    @"tier":@"tier",
                                    @"sessionId":@"sessionId",
                                    @"pass":@"password"
                                    };

userObjectMapping.identificationAttributes = @[@"id"];

[userObjectMapping addAttributeMappingsFromDictionary:userObjectMappingDict];

RKEntityMapping* userBusinessMapping =  [RKEntityMapping mappingForEntityForName:@"Business" inManagedObjectStore:objectManager.managedObjectStore];
[userBusinessMapping addAttributeMappingsFromDictionary:@{@"business":@"id"}]; // Nil Key path

[userObjectMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"business" withMapping:userBusinessMapping]];

最佳答案

您应该有一个尚未显示的 RKRequestDescriptor 实例。它是使用 requestDescriptorWithMapping:objectClass:rootKeyPath: 创建的。您已将根 key 路径设置为 @"user",您应该将其设置为 nil。

关于ios - RestKit:如何在没有任何嵌套属性的情况下将 NSManagedObject 作为 JSON 发布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20040236/

相关文章:

iphone - 如何在iOS上设计正确的MVC?

iphone - 从另一个 View Controller 与另一个 View Controller 交互

javascript - Objective-C 方法/函数调用。 (来自 JavaScript 示例)

ios - Yelp API ios 获取更大的图像

ios - 访问 RestKit 请求体

ios - Pod 更新后找不到“Parse/PFFile.h”文件

ios - Firebase 3x 方法不适用于真实设备但适用于模拟器 Swift 3.0

java - 如何对依赖JSON的应用进行离线测试?

ios - 错误 :Passing address of non-scalar object to __autoreleasing parameter for write-back

iphone - 蓝牙警告 Xcode?