我是使用Swift进行编码的新手,我正在尝试将两个文本字段写入我的核心数据文件中。 我通过在线搜索找到了我需要的大部分代码,但是我收到了一个我无法弄清楚的错误。 我已经发布了下面的整个保存功能代码但我得到一个“不能在类型'NSManagedObjectContext'的非可选值上使用可选链接”错误在以managedObjectContext开头的行上。
@IBAction func saveData(sender: AnyObject) { let entityDescription = NSEntityDescription.entityForName("MatchData", inManagedObjectContext: managedObjectContext) let matchData = MatchData(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext) matchData.teamNumber = teamNumber.text matchData.matchNumber = matchNumber.text var error: NSError? managedObjectContext?.save(error) if let err = error { status.text = err.localizedFailureReason } else { teamNumber.text = "" matchNumber.text = "" } }我正在使用最新版本的Xcode,我认为这个问题与Swift中的变化有关,但由于我是新手,我无法弄明白。 任何帮助调试将不胜感激。
I am new to coding with Swift and I am trying to write two text fields into my core data file. I have figured out most of the code I need by searching on line but I am getting an error that I can't figure out. I have posted the entire save function code below but I am getting a "Cannot use optional chaining on non-optional value of type 'NSManagedObjectContext'" error on the line starting with managedObjectContext.
@IBAction func saveData(sender: AnyObject) { let entityDescription = NSEntityDescription.entityForName("MatchData", inManagedObjectContext: managedObjectContext) let matchData = MatchData(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext) matchData.teamNumber = teamNumber.text matchData.matchNumber = matchNumber.text var error: NSError? managedObjectContext?.save(error) if let err = error { status.text = err.localizedFailureReason } else { teamNumber.text = "" matchNumber.text = "" } }I am working with the newest version of Xcode and I think the issue has something to with the changes in Swift but I can't figure it out since I am a novice. Any help debugging would be appreciated.
最满意答案
do, try, catch语句在哪里? 如果要将对象保存到managedObjectContext ,则必须使用此语句,如下所示: do { try manaagedObjectContext.save() } catch { // handle error }
请试一试! 希望能帮助到你
`@IBAction func saveData(sender:AnyObject){
让entityDescription:MatchData = NSEntityDescription.insertNewObjectForEntityForName(“MatchData”,inManagedObjectContext:managedObjectContext)为! MatchData
entityDescription.teamNumber = self.teamNumber.text entityDescription.matchNumber = self.matchNumber.text do { try managedObjectContext.save() } catch { /*Handle error*/ }print(entityDescription)}`
Where is the do, try, catch statement? If you want to save objects to the managedObjectContext you must be use this statement like so:do { try manaagedObjectContext.save() } catch { // handle error }
Please try this out! Hope it helps
`@IBAction func saveData(sender: AnyObject) {
let entityDescription:MatchData = NSEntityDescription.insertNewObjectForEntityForName("MatchData", inManagedObjectContext: managedObjectContext) as! MatchData
entityDescription.teamNumber = self.teamNumber.text entityDescription.matchNumber = self.matchNumber.text do { try managedObjectContext.save() } catch { /*Handle error*/ }print(entityDescription) }`
更多推荐
发布评论