gpt4 book ai didi

xcode - 编辑核心数据对象时发生 fatal error

转载 作者:行者123 更新时间:2023-11-30 13:25:09 24 4
gpt4 key购买 nike

我是编程新手。使用 CoreData,我尝试创建函数来编辑输入文本字段中的数据,以编辑要保存到核心数据的数据。我遇到 fatal error :

unexpectedly found nil while unwrapping an Optional value

@IBAction func update(sender: AnyObject) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

let manageContext = appDelegate.managedObjectContext

let fetchRequest = NSFetchRequest(entityName: "Coursework")

do {

let results = try manageContext.executeFetchRequest(fetchRequest)

let attribute = results[0] as! NSManagedObject

detailItem?.value = modulename.text

attribute.setValue(courseworkname.text, forkey: "courseworkname")
attribute.setValue(dueDateLabel.text, forkey: "duedate")
attribute.setValue(level.text, forkey: "level")
attribute.setValue(mark.text, forkey: "mark")
attribute.setValue(modulename.text, forkey: "modulename")
attribute.setValue(notes.text, forkey: "notes")
attribute.setValue(progressbar.text, forkey: "progressbar")
attribute.setValue(reminder.text, forkey: "reminder")
attribute.setValue(value.text, forkey: "value")

try manageContext.save()

}catch let error as NSError {
}
}
<小时/>

完整代码:

    @IBOutlet weak var detailDescriptionLabel: UILabel!
@IBOutlet weak var dueDateLabel: UITextField!
@IBOutlet weak var value: UITextField!
@IBOutlet weak var courseworkname: UITextField!
@IBOutlet weak var modulename: UITextField!
@IBOutlet weak var level: UITextField!
@IBOutlet weak var mark: UITextField!
@IBOutlet weak var reminder: UITextField!
@IBOutlet weak var notes: UITextField!

@IBAction func edit(sender: AnyObject) {

modulename.userInteractionEnabled = true
modulename.enabled = true
dueDateLabel.userInteractionEnabled = true
value.userInteractionEnabled = true
modulename.userInteractionEnabled = true
level.userInteractionEnabled = true
mark.userInteractionEnabled = true
reminder.userInteractionEnabled = true
notes.userInteractionEnabled = true

//Interaction

value.enabled = true
dueDateLabel.enabled = true
courseworkname.enabled = true
modulename.enabled = true
level.enabled = true
mark.enabled = true
reminder.enabled = true
notes.enabled = true

}

@IBAction func update(sender: AnyObject) {

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

let manageContext = appDelegate.managedObjectContext

let fetchRequest = NSFetchRequest(entityName: "Coursework")

do {

let results = try manageContext.executeFetchRequest(fetchRequest)


let attribute = results[0] as! NSManagedObject

detailItem?.value = modulename.text

attribute.setValue(courseworkname.text, forKey: "courseworkname")
attribute.setValue(dueDateLabel.text, forKey: "duedate")
attribute.setValue(level.text, forKey: "level")
attribute.setValue(mark.text, forKey: "mark")
attribute.setValue(modulename.text, forKey: "modulename")
attribute.setValue(notes.text, forKey: "notes")
attribute.setValue(progressbar.text, forKey: "progressbar")
attribute.setValue(reminder.text, forKey: "reminder")
attribute.setValue(value.text, forKey: "value")



try manageContext.save()

}catch let error as NSError{

}

}

var detailItem: Coursework?
var detailItem2: Task?

{
didSet {
// Update the view.
self.configureView()
}
}

func configureView() {
// Update the user interface for the detail item.
if let detail = self.detailItem {
if let label = self.detailDescriptionLabel {
label.text = detail.courseworkname
}
if let label = self.dueDateLabel {
label.text = detail.duedate
}

if let label = self.value {
label.text = detail.value
}

if let label = self.courseworkname {
label.text = detail.courseworkname
}

if let label = self.modulename {
label.text = detail.modulename
}

if let label = self.level {
label.text = detail.level
}

if let label = self.mark {
label.text = detail.mark
}

if let label = self.reminder{
label.text = detail.reminder
}

if let label = self.notes{
label.text = detail.notes

}

}

最佳答案

这是一个新属性吗?当您更改 CoreData 模型时,您必须卸载并重新安装该应用。

还刚刚看到您没有为进度条定义 IBOutlet,如果链接它它应该可以工作。

关于xcode - 编辑核心数据对象时发生 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37310700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com