0
嘗試在單元格中創建textLabels數組時,出現此運行時錯誤。Swift - 核心數據運行時錯誤
我的代碼如下所示:
else {
mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
mySelectedCell.tintColor = UIColor.blackColor()
if let tx = mySelectedCell.textLabel?.text as Optional?{
var textLabel:String = String()
textLabel = tx!
var tempFriend = Model(entity: en!, insertIntoManagedObjectContext: context)
//Save user to core data
tempFriend.tempUser = textLabel
//Save context
context.save(nil)
//Make list from objects
liste = context.executeFetchRequest(freq, error: nil)!
//Make new list of strings from first list
for var i = 0; i < liste.count; ++i{
var data:NSManagedObject = liste[i] as NSManagedObject
//The next line is where the error appears
showList.append(data.valueForKeyPath("tempUser") as String)
}
//Show list
println(showList)
}
}
}
我的錯誤說:
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb).
我不明白爲什麼有些會在我的代碼返回"nil"
。
任何想法,將不勝感激。
您不應該試圖將mySelectedCell.textLabel?.text作爲可選項來投射。 if let語句的目的是解開可選的,所以'as Optional?'是完全不必要的 – Ian