gpt4 book ai didi

objective-c - 使用 RestKit 将 bool 属性从 JSON 映射到 NSManagedObject

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:03 24 4
gpt4 key购买 nike

我正面临着一些神秘的事情。我在 NSManagedObject 子类“ReinspectionTask”上有一个 bool 属性。属性为“isOpen”,表示任务是否还需要完成。在 Core Data 中,属性类型设置为 bool 值。该属性被声明为 NSNumber。我从服务器中提取 JSON,它看起来像这样:

  [
{
"jobID":"73",
"address":"500 17th Ave. ",
"city":"Santa Cruz",
"state":"California",
"inspections":[
{
"inspectionID":"186",
"inspectionType":"Foundation",
"mediaKey":"777",
"hasTasks":true,
"requestSubmitted":false,
"reinspectionTasks":[
{
"taskID":"75",
"title":"Foundation re-inspection item",
"requestText":"Need a photo of <something>",
"isOpen":false
},
"wasApproved":false,
"isScheduled":false,
"hasAvailableTimes":false,
"availableTimes":[]

]
}
]
}
]

然后我使用带有以下代码的 RestKit 将 JSON 映射到我的托管对象模型:

        RKManagedObjectMapping *jobMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Job"];
RKManagedObjectMapping *inspectionMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Inspection"];
RKManagedObjectMapping *reinspectionTaskMapping = [RKManagedObjectMapping mappingForEntityWithName:@"ReinspectionTask"];

jobMapping.primaryKeyAttribute = @"jobID";
[jobMapping mapKeyPath:@"address" toAttribute:@"address"];
[jobMapping mapKeyPath:@"city" toAttribute:@"city"];
[jobMapping mapKeyPath:@"state" toAttribute:@"state"];
[jobMapping mapKeyPath:@"jobID" toAttribute:@"jobID"];
[jobMapping mapRelationship:@"inspections" withMapping:inspectionMapping];
[[RKParserRegistry sharedRegistry] setParserClass:[[RKParserRegistry sharedRegistry]


parserClassForMIMEType:RKMIMETypeJSON]
forMIMEType:@"text/html"];

inspectionMapping.primaryKeyAttribute = @"inspectionID";
[inspectionMapping mapKeyPathsToAttributes:
@"inspectionID", @"inspectionID",
@"inspectionTime", @"inspectionTime",
@"inspectionType", @"inspectionType",
@"inspector", @"inspector",
@"isScheduled", @"isScheduled",
@"mediaKey", @"mediaKey",
@"requestSubmitted", @"requestSubmitted",
@"hasAvailableTimes", @"hasAvailableTimes",
@"availableTimes", @"availableTimes",
@"hasTasks", @"hasTasks",
@"wasApproved", @"wasApproved", nil];
[inspectionMapping mapRelationship:@"reinspectionTasks" withMapping:reinspectionTaskMapping];
[inspectionMapping mapRelationship:@"job" withMapping:jobMapping];

reinspectionTaskMapping.primaryKeyAttribute = @"taskID";
[reinspectionTaskMapping mapKeyPath:@"taskID" toAttribute:@"taskID"];
[reinspectionTaskMapping mapKeyPath:@"title" toAttribute:@"title"];
[reinspectionTaskMapping mapKeyPath:@"requestText" toAttribute:@"requestText"];
[reinspectionTaskMapping mapKeyPath:@"isOpen" toAttribute:@"isOpen"];
[reinspectionTaskMapping mapRelationship:@"inspection" withMapping:inspectionMapping];

[objectManager.mappingProvider setMapping:jobMapping forKeyPath:@"job"];
[objectManager.mappingProvider setMapping:inspectionMapping forKeyPath:@"inspection"];
[objectManager.mappingProvider setMapping:reinspectionTaskMapping forKeyPath:@"reinspectionTasks"];

这在大多数情况下都可以正常工作。但是,我对 ReinspectionTask 上的 isOpen BOOL 属性有疑问。当我将该值记录到控制台时,它返回 null。奇怪的是,据我所知,我已经像 hasTasks、isScheduled 等一样设置了该属性。这些 BOOL 属性工作正常。

我应该提到 Inspection 与 ReinspectionTasks 的关系是一对多关系。这种关系是否需要我采取一些其他步骤来设置 RestKit 中的对象映射?

或者可能与 ReinspectionTask 对象在 JSON 结构中嵌套得更深有关?

我们非常欢迎任何想法或建议。非常感谢。

最佳答案

好吧,只有两个问题很容易回答。

Does that kind of relationship require that I take some other step in setting up the object mapping in RestKit?

不,你似乎做得对。

Or might it have to do with the fact that the ReinspectionTask object is nested deeper in the JSON structure?

不,这不是问题所在。正如我在上面的评论中提到的,我怀疑你做错了什么。 BOOL 属性永远不应在控制台中显示为 null。我会检查目标对象的所有属性,并检查它们在数据模型和实体类(如果有的话)中的类型是否正确。我还会在您的数据模型中检查此属性的默认值

关于objective-c - 使用 RestKit 将 bool 属性从 JSON 映射到 NSManagedObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11024836/

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