[英]how to save 2 attributes using 1 function to core Data (Swift4)
My code right now is causing a runtime error. 我的代码现在导致运行时错误。 It works perfectly with just 1 attribute but with 2 it crashes.
它仅与1个属性完美配合,但有2个崩溃。 I have also attached a photo of my coreData file below.
我还在下面附上了我的coreData文件的照片。
class firstVC : UIViewController {
@IBOutlet var enterText: UITextField!
@IBOutlet var enterNumber: UITextField!
var itemsName : [NSManagedObject] = []
var textFieldz : UITextField!
@IBAction func sam(){
save()
}
func save() {
let appD = UIApplication.shared.delegate as! AppDelegate
let context = appD.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Team", in : context)!
let theTitle = NSManagedObject(entity: entity, insertInto: context)
theTitle.setValue(enterText.text, forKey: "lorde")
theTitle.setValue(enterNumber.text, forKey: "num")
do {
try context.save()
itemsName.append(theTitle)
////
}
catch {
print("Tom Corley")
}
}}
I see you have tagged your question swift4 so why not work directly with the entity class 我看到您已标记问题swift4,为什么不直接使用实体类
let team = Team(context: context)
team.lorde = enterText.text
team.num = enterNumber.text
This way you avoid any spelling error and can find errors at compile time rather than runtime. 这样,您可以避免任何拼写错误,并且可以在编译时而不是运行时发现错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.