我定义了三个类:
咖啡品牌:
dynamic var brandName = ""
let brands = List<Coffee>()
咖啡:
dynamic var name = ""
let cupAmount = List<CoffeeCup>()
咖啡杯:
dynamic var cup = 0
dynamic var caffeine = 0
并且想尝试一下,所以我添加了一个品牌星巴克来尝试它在查看的表格中的样子
class CoffeeListsTableViewController: UITableViewController {
// MARK: Properties
var brands: Results<CoffeeBrand>!
override func viewDidLoad() {
super.viewDidLoad()
let Starbucks = CoffeeBrand(value: ["Starbucks",["Iced Latte", [200,150],[500,150]]])
try! realm.write() {
realm.add(Starbucks)
}
}
}
但是我得到的是“线程1:信号SIGABRT”,我检查了一下,我认为这应该在没有线程1的情况下工作(我在谷歌上搜索并尝试从其他人的问题中找出问题所在)
与此表中的数据源:
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return brands.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
//let cellIdentifier = "CoffeeBrandTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: "CoffeeBrandTableViewCell", for: indexPath) as! CoffeeBrandTableViewCell
// in my cell, only a label of brandName
let brandList = brands[indexPath.row]
cell.brandName.text = brandList.brandName
return cell
}
最佳答案
从该代码的外观来看,您从未为品牌
分配值。由于您将品牌明确声明为未包装,因此这可能会导致问题。
class CoffeeListsTableViewController: UITableViewController {
// MARK: Properties
let brands = try! Realm().objects(CoffeeBrand.self)
override func viewDidLoad() {
super.viewDidLoad()
}
}
顺便说一句,realm.add
每次调用时都会向 Realm 数据库添加一个新对象。在当前代码中,每次加载 View Controller 时,您都会添加 Starbucks 对象的新副本。在向 Realm 添加对象时添加更多控制会更合适。
关于Swift 和 Realm : getting Thread 1: signal SIGABRT when add objects in list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42616546/