Xpre 7更新: Xpre 7 beta 2已解决了此问题。可选的Core
Data属性现在定义为Xpre生成的托管对象子类中的可选属性。不再需要编辑生成的类定义。
(上一个答案:)
创建
NSManagedObject子类时,Xpre不会为在Core
Data模型检查器中标记为“可选”的那些属性定义可选属性。在我看来,这似乎是个虫子。
解决方法是,可以将属性强制转换为可选属性(视
as String?情况而定),然后使用可选绑定进行测试
if let notice = someManagedObject.noticeText as String? { println(notice)} else { // property not set}在你的情况下
if let notice = formQuestions[indexPath.row].noticeText as String? { println(notice)} else { // property not set}更新: 从Xpre6.2开始,此解决方案不再起作用,并因EXC_BAD_ACCESS运行时异常而崩溃(比较Swift:在运行时以非可选方式检测到意外的nil值:强制转换为可选失败
下面的“旧答案”解决方案仍然有效。
(旧答案:)
正如@ Daij-Djan在评论中已经指出的那样,您必须将可选的Core Data属性的属性定义为 可选 或 隐式展开的optional :
@NSManaged var noticeText: String? // optional@NSManaged var noticeText: String! // implicitly unwrapped optional
不幸的是,在创建NSManagedObject子类时Xpre不能正确定义可选属性,这意味着如果在模型更改后再次创建子类,则必须重新应用更改。
同样,这似乎仍然没有记录,但是这两种变体在我的测试案例中都起作用。
您可以使用以下方法测试属性
== nil:
if formQuestions[indexPath.row].noticeText == nil { // property not set}或带有可选的分配:
if let notice = formQuestions[indexPath.row].noticeText { println(notice)} else { // property not set}


