【发布时间】:2015-10-19 19:55:09
【问题描述】:
我收到此错误消息:“条件绑定的初始化程序必须具有可选类型,而不是 'NSManagedObjectContext”。
我不确定如何修复此错误。我认为错误在于“如果让”。
if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
restaurant = NSEntityDescription.insertNewObjectForEntityForName("Restaurant",
inManagedObjectContext: managedObjectContext) as! Restaurant
restaurant.name = nameTextField.text
restaurant.type = typeTextField.text
restaurant.location = locationTextField.text
restaurant.image = UIImagePNGRepresentation(imageView.image!)
restaurant.isVisited = isVisited
//restaurant.isVisited = NSNumber.convertFromBooleanLiteral(isVisited)
var e: NSError?
if managedObjectContext.save() != true {
print("insert error: \(e!.localizedDescription)")
return
}
}
【问题讨论】: