Swift + CoreData:无法在生成的NSManagedObject子类上自动设置可选属性 [英] Swift + CoreData: Cannot Automatically Set Optional Attribute On Generated NSManagedObject Subclass

查看:152
本文介绍了Swift + CoreData:无法在生成的NSManagedObject子类上自动设置可选属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Record的coredata实体,其属性为dateUpdated.我注意到生成的NSManagedObject子类没有可选标记(?)

I have a coredata entity named Record and has a property dateUpdated. I noticed that the generated NSManagedObject subclass has no optional mark (?)

CoreData编辑器:

生成的子类:

预期:

更新: 就我而言,这很麻烦,因为每次我想重新生成子类时,这意味着我还需要手动更新所有可选值.子类中具有非可选(不带?")项,导致我在分配之前检查evalue,如下例所示:

UPDATED: It's tedious in my part, because each time I want to regenerate the subclasses, it means I also need to update all optional values manually. Having a non-optional (without '?') in subclass lead me to check evalue before assigning, like example below:

// 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可以使用(?)在子类中自动设置可选值,我只需要这样做:

But if xcode can automatically set optional value in subclass with (?) I just need to do like this:

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

就我而言,我有一堆属性需要手动标记为可选.

In my case, I have bunch of properties I need to mark as optional manually.

推荐答案

在Swift及其引入的可选选项之前,Core Data Model Editor中的可选复选框已存在.苹果在其

The optional checkbox in the Core Data Model Editor has been existing before Swift and its optionals where introduced. Apple states about it in its Core Data Programming Guide:

您可以指定属性是可选的-即,它不是 要求具有价值.但是总的来说,您不建议这样做 这样做-特别是对于数值(通常您可以得到 使用具有默认值的强制属性可以得到更好的结果-在 型号-的0).原因是SQL具有特殊的比较 NULL的行为与Objective-C的nil不同.数据库中为NULL 与0不同,并且搜索0将不匹配具有以下内容的列 空.

You can specify that an attribute is optional — that is, it is not required to have a value. In general, however, you are discouraged from doing so — especially for numeric values (typically you can get better results using a mandatory attribute with a default value — in the model — of 0). The reason for this is that SQL has special comparison behavior for NULL that is unlike Objective-C's nil. NULL in a database is not the same as 0, and searches for 0 will not match columns with NULL.

因此,使用Swift和Xcode 8,(仍)可选复选框与您是否将属性定义为可选属性或是否不在ManagedObject子类中这一事实相关.并且不要期望在使用 Editor >创建子选项卡时,可选"复选框会对NSManagedObject子类属性的可选类型产生任何影响. 创建NSManagedObject子类.

Thus, using Swift and Xcode 8, optional checkbox is (still) not related to the fact that you defined your properties as optionals or not in your managedObject subclasses. And don't expect optional checkbox to have any impact on your NSManagedObject subclasses properties optional type when you create them using Editor > Create NSManagedObject Subclass.

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

That said, every time I need a property to have its optional checkbox checked in the Model Editor, I immediately put its NSManagedObject Subclass declaration as an optional.

生成器每次都能自动将NSManagedObject子类属性从非可选切换为可选您可以在数据模型检查器中为每个实体的属性更改此选项,然后重新构建项目.

Mogenerator is able to automatically toggle your NSManagedObject subclass properties from non-optional to optional every time you change this option for each of your Entity's attribute in the Data Model Inspector and rebuild your project.

这篇关于Swift + CoreData:无法在生成的NSManagedObject子类上自动设置可选属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆