Swift: handling an unexpected nil value, when variable is not optional [duplicate]

房东的猫 提交于 2019-12-06 12:01:18

Since you have nil values, the title property should be optional and you should declare it as optional in your core data model and in your NSManagedObject historyItem class.

When I do this in editor I dont get an error so might be the answer:

let a = ""

if a as String? == nil {
        println("Nil")
    }
    else {
        println("Not nil")
    }

@NSManaged var title: String should be @NSManaged var title: String? if there is possibility for a nil value. Then, you cannot go wrong with optional binding:

if let historyItemTitle = historyItem.title {
  cell?.textLabel?.text = historyItemTitle
} else {
  cell?.textLabel?.text = "Title missing"
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!