gpt4 book ai didi

ios - 如何将多个条目保存到一个 nsmangedObject

转载 作者:行者123 更新时间:2023-11-29 05:58:31 24 4
gpt4 key购买 nike

我正在尝试将 2 个不同的字符串实体保存到核心数据中。然后用表格 View 显示我下面的函数保存数据。现在代码显示在不同的表格 View 单元格上。意味着实体 a 正在显示在单元格 1 上,实体 b 正在显示在单元格 2 上。它们没有被打印到同一单元格中。

var itemName2 : [NSManagedObject] = []
var itemName : [NSManagedObject] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemName.count
}

func enterData() {
let appDeldeaget = UIApplication.shared.delegate as! AppDelegate
let context = appDeldeaget.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Data", in: context)

let theTitle = NSManagedObject(entity: entity!, insertInto: context)
theTitle.setValue(hits.text, forKey: "hits")

let theTitle2 = NSManagedObject(entity: entity!, insertInto: context)
theTitle2.setValue(atBats.text, forKey: "atBATS")

do {
try context.save()
itemName.append(theTitle)
itemName.append(theTitle2)
} catch {
print("d")
}
self.theScores.reloadData()

hits.text = ""
hits.resignFirstResponder()
}

最佳答案

试试这个,

func enterData() {
let appDeldeaget = UIApplication.shared.delegate as! AppDelegate
let context = appDeldeaget.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Data", in: context)

let theTitle = NSManagedObject(entity: entity!, insertInto: context)
theTitle.setValue(hits.text, forKey: "hits")

//removed theTitle2 and set the second value in theTitle itself
theTitle.setValue(atBats.text, forKey: "atBATS")

do {
try context.save()
itemName.append(theTitle)
//add theTitle only
} catch {
print("d")
}
self.theScores.reloadData()
hits.text = ""
hits.resignFirstResponder()
}

关于ios - 如何将多个条目保存到一个 nsmangedObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54896521/

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