gpt4 book ai didi

ios - swift + 核心数据 : Cannot Automatically Set Optional Attribute On Generated NSManagedObject Subclass

转载 作者:IT王子 更新时间:2023-10-29 05:00:46 25 4
gpt4 key购买 nike

我有一个名为 Record 的核心数据实体,并且有一个属性 dateUpdated。我注意到生成的 NSManagedObject 子类没有可选标记(?)

CoreData 编辑器:

enter image description here

生成的子类:

enter image description here

预期:

enter image description here

更新:这对我来说很乏味,因为每次我想重新生成子类时,这意味着我还需要手动更新所有可选值。在子类中有一个非可选的(没有'?')让我在分配之前检查评估值,如下例所示:

// sample value:
// serverDateFormatter = "yyyy/MM/dd"
// dateString = ""

// Branch is a subclass of Record (see above images)

var date = self.coreData.serverDateFormatter.dateFromString(dateString)
if date != nil
{
branch.dateUpdated = date
}

但是如果 xcode 可以使用 (?) 在子类中自动设置可选值,我只需要这样做:

branch.dateUpdated = self.coreData.serverUTCDateFormatter.dateFromString(dateString)

在我的例子中,我有一堆属性需要手动标记为可选。

最佳答案

核心数据模型编辑器中的可选复选框在引入 Swift 及其可选项之前就已存在。 Apple 在其 Core Data Programming Guide 中对此进行了说明:

You can specify that an attribute is optional — that is, it is notrequired to have a value. In general, however, you are discouragedfrom doing so — especially for numeric values (typically you can getbetter results using a mandatory attribute with a default value — in themodel — of 0). The reason for this is that SQL has special comparisonbehavior for NULL that is unlike Objective-C's nil. NULL in a databaseis not the same as 0, and searches for 0 will not match columns withNULL.

因此,使用 Swift 和 Xcode 8,可选复选框(仍然)与您是否在 managedObject 子类中将属性定义为可选属性这一事实无关。当您使用 Editor> Create NSManagedObject Subclass 创建它们时,不要期望可选复选框对您的 NSManagedObject 子类属性可选类型有任何影响。

就是说,每次我需要一个属性在模型编辑器中选中它的可选复选框时,我都会立即将其 NSManagedObject 子类声明作为可选。


附录

Mogenerator每次您在数据模型检查器中为每个实体的属性更改此选项并重建项目时,都能够自动将您的 NSManagedObject 子类属性从非可选切换为可选。

关于ios - swift + 核心数据 : Cannot Automatically Set Optional Attribute On Generated NSManagedObject Subclass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25485273/

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