2017-03-06 50 views
0

添加对象我定义了三类:斯威夫特和境界:让线程1:信号SIGABRT当列表

CoffeeBrand:

dynamic var brandName = "" 
let brands = List<Coffee>() 

咖啡:

dynamic var name = "" 
let cupAmount = List<CoffeeCup>() 

的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(这是我谷歌合作并试图找到什么是从别人的错误questiones)

随着该表中的数据源:

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 
} 

回答

0

从代码的外观,你不是永远值分配给brands。由于您宣称品牌明确展开,可能会导致问题。

class CoffeeListsTableViewController: UITableViewController { 

    // MARK: Properties 
    let brands = try! Realm().objects(CoffeeBrand.self) 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 
} 

另外,realm.add在每次调用Realm数据库时都会添加一个新对象。在您当前的代码中,每当视图控制器加载时,您都会添加星巴克对象的新副本。在向Realm添加对象时添加更多控制会更加合适。

+0

不,这不是问题,我以前试过,它会得到“线程1:EXC_BAD_INSTRUCTION” – whatever123