使用Swift写入核心数据时出现错误代码的问题(Problems with error code when writing to core data with Swift)

编程入门 行业动态 更新时间:2024-10-23 04:48:10
使用Swift写入核心数据时出现错误代码的问题(Problems with error code when writing to core data with Swift)

我是使用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) }`

更多推荐

本文发布于:2023-07-24 01:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240117.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出现错误   核心   代码   数据   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!