gpt4 book ai didi

ios - RestKit: 'Unacceptable type of value for to-one relationship: property = " Prop ”;所需类型 = A 型;给定类型 = TypeS;

转载 作者:行者123 更新时间:2023-11-29 12:59:52 25 4
gpt4 key购买 nike

我正在重构我的应用程序的最新版本以使用 RestKit。

在解析我得到的JSON数据时,我遇到了以下错误:

'一对一关系 Not Acceptable 值类型:property = “business”;所需类型 = 业务;给定类型 = 业务;

错误发生在我有之后

  1. 成功发布对象
  2. 成功得到我的反馈JSON

弹出的错误解析了大部分 JSON 并准备设置 JSON 中传递的对象之间的关系。

我发送的 JSON(用于登录):

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

我拿回来的字典:

{
business = 2;
email = "<null>";
firstName = "<null>";
id = 36;
lastName = "<null>";
login = "<null>";
pass = "<null>";
phoneNumber = "<null>";
sessionId = B0F4E25AF15639D09E49BB9A1F179847;
tier = 0;
}

基本上我只得到我的用户 ID、 session ID 和我的用户帐户绑定(bind)的企业 ID。

但是,User 对象在 CoreData 模型 中的定义略有不同。

区别在于“业务”。在 JSON 通信中,它只是一个业务 ID。但在我的本地 CoreData 模型 中,它是与完整业务对象关系

如果我们在Dictionary中写CoreData Entity Definition,它更像是:

{
business = {
address = "addr";
category = 2;
email = "company@email.com";
id = 2;
name = @"Acme";
phoneNumber = @"911";
postalCode = @"98105";
.........
}

email = "myemail@company.com";
firstName = "James";
id = 36;
lastName = "Bond";
login = "awesome_guy";
pass = "42";
phoneNumber = "911";
sessionId = B0F4E25AF15639D09E49BB9A1F179847;
tier = 0;
}

因此,这就是我为用户和企业配置映射的方式:

/* ===============================
* ====Business Object Mapping====
* ==============================*/

RKEntityMapping* businessObjectMapping = [RKEntityMapping mappingForEntityForName:@"Business" inManagedObjectStore:objectManager.managedObjectStore];

[businessObjectMapping setPerformKeyValueValidation:NO];

NSDictionary *businessObjectMappingDict = @{
@"id":@"id",
@"name":@"name",
@"address":@"address",
@"category":@"category",
@"email":@"email",
@"integration":@"integration",
@"phoneNumber":@"phoneNumber",
@"postalCode":@"postalCode",
};
businessObjectMapping.identificationAttributes = @[@"id"];

[businessObjectMapping addAttributeMappingsFromDictionary:businessObjectMappingDict];

这是我为用户配置映射的方式:

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

RKEntityMapping* userObjectMapping = [RKEntityMapping mappingForEntityForName:@"User" 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]];

[userObjectMapping setPerformKeyValueValidation:NO];

当然,我已经为调用注册了用户映射:

/*******************************************************************
* Object Mapping Registration *
*******************************************************************/

/* Consult API Doc for calls */
// Only used called included

[objectManager addResponseDescriptorsFromArray:@[

......

[RKResponseDescriptor responseDescriptorWithMapping:userObjectMapping method:RKRequestMethodPOST
pathPattern:@"login" keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
......
]];

仅供引用,如果重要的话,我正在使用 Mogenerator 生成 MO 文件。

当然,我已经为实体指定了类。

这个错误基本上是在说“你给了我我想要的,所以你错了”。它看起来更像是 CoreData 错误而不是 RestKit 错误。

感谢您的帮助。

最佳答案

问题出在您提供的 RestKit 映射上,因为您试图在一个响应描述符中执行所有操作(将其视为目标是数组,而不是关系)。

因此,您需要将映射分解为 2 个响应描述符。 1 个用于用户,1 个用于业务。然后你需要使用外键映射来连接这两个对象。这意味着将业务 ID 作为 transient 属性添加到用户对象并使用:

[userObjectMapping addConnectionForRelationship:@"business" connectedBy:@{ @"businessId": @"businessId" }];

请注意,我已指定 businessId,因为您的托管对象实际上不应该具有 id 属性,因此您应该更改为 userIdbusinessId.

关于ios - RestKit: 'Unacceptable type of value for to-one relationship: property = " Prop ”;所需类型 = A 型;给定类型 = TypeS;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20060904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com