我一直在尝试做一个简单的CoreData任务,保存数据。我确定它可以在Beta 6中运行,但在更新到Beta 7后开始出现错误。Beta 7中的XCode 6 Beta 6错误 - 可选类型的值未解包
我想我必须添加'?'要么 '!'基于错误提示,但只是不够聪明,弄清楚哪里!
@IBAction func saveItem(sender: AnyObject) {
// Reference to App Delegate
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
// Reference our moc (managed object content)
let contxt: NSManagedObjectContext = appDel.managedObjectContext!
let ent = NSEntityDescription.entityForName("List", inManagedObjectContext: contxt)
// Create instance of our data model and initialize
var newItem = Model(entity: ent, insertIntoManagedObjectContext: contxt)
// Map our attributes
newItem.item = textFieldItem.text
newItem.quanitity = textFieldQuantity.text
newItem.info = textFieldInfo.text
// Save context
contxt.save(nil)
}
错误说
Value of optional type 'NSEntityDescription?' not unwrapped; did you mean to use '!' or '?'
在生产线
var newItem = Model(entity: ent, insertIntoManagedObjectContext: contxt)
每次我似乎有明显的错误,并编译OK,点击“保存”显示了在调试区
fatal error: unexpectedly found nil while unwrapping an Optional value