【发布时间】:2015-12-09 01:50:49
【问题描述】:
我正在尝试将 UITextField 中的一些文本保存到 xcode 7 中的 CoreData,swift 2.0
@IBOutlet weak var titleName: UITextField!
@IBOutlet weak var problemDescription: UITextField!
@IBOutlet weak var targetMarket: UITextField!
//Save information to core data
@IBAction func saveInformation(sender: AnyObject) {
print("Attempting to save!")
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
let newIdea = NSEntityDescription.insertNewObjectForEntityForName("SaveIdea", inManagedObjectContext: context)
print("\(titleName) is cool")
newIdea.setValue("\(titleName)", forKey: "title")
newIdea.setValue("\(problemDescription)", forKey: "targetMarket")
newIdea.setValue("\(targetMarket)", forKey: "problemSolved")
do{
try context.save()
print("Save Success!")
}catch {
print("Error!")
}
}
但是,它将信息存储为 nil,当我尝试使用 (titleName.text!, forKey:...) 时(例如)它给了我一个错误 - 尝试解开和可选的 nil -
有人知道解决办法吗?提前谢谢你:)
【问题讨论】: