Swift: detecting an unexpected nil value in a non-optional at runtime: casting as optional fails

后端 未结 1 1561
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-22 00:19

I have a UITableViewController loading its entries from Core Data via a NSFetchedResultsController. Like this:

let historyItem = fe         


        
相关标签:
1条回答
  • 2020-12-22 00:56

    I think you should make your title an optional if core data can return nil value for title

    @NSManaged var title: String?
    

    And test it without the cast

    if let possibleTitle = historyItem.title{
        NSLog("possibleTitle was set OK")
    } else {
        NSLog("possibleTitle was nil")
    }
    
    0 讨论(0)
提交回复
热议问题