2017-03-06 48 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