ios - Make a List with some of the items from Results in Realm Swift -


what want make list items results. have uitableview populated results. table want create list when user selects rows, reason nothing gets appended list when append items results list when rows selected.

here code have:

realm object:

import foundation import realmswift  class fruit:object{     dynamic var fruitname:string = ""     dynamic var createdat = nsdate() } 

viewcontroller:

var fruits: results<fruit>! var fruitselectionlist: list<fruit>!  override func viewdidload() {     super.viewdidload()      updatefruits()      tablefruits.setediting(true, animated: true)     tablefruits.allowsmultipleselectionduringediting = true }      func updatefruits(){     fruits = realm.objects(fruit.self).sorted(bykeypath: "fruitname") }  func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {     return fruits.count }  func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) {      if let index = fruitselectionlist?.index(of: fruits[indexpath.row]) {         try! realm.write {             fruitselectionlist?.remove(at: index)         }     } else {         try! realm.write {             // nothing gets appended here, why?             fruitselectionlist?.append(fruits[indexpath.row])         }     } } func tableview(_ tableview: uitableview, diddeselectrowat indexpath: indexpath) {     if let index = fruitselectionlist?.index(of: fruits[indexpath.row]) {         fruitselectionlist?.remove(at: index)     } } 

the funny thing similar code without using realm works fine...

plain swift code works when not using realm:

class viewcontroller: uiviewcontroller, uitableviewdatasource, uitableviewdelegate  {      var fruits = ["apples", "oranges", "grapes", "watermelon", "peaches"]      var newfruitlist:[string] = []      @iboutlet weak var tableview: uitableview!      override func viewdidload() {         super.viewdidload()          tableview.allowsselection = false         tableview.setediting(true, animated: true)         tableview.allowsmultipleselectionduringediting = true     }      func numberofsections(in tableview: uitableview) -> int {         return 1     }      func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {         return fruits.count     }      func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {           let cell = uitableviewcell()         cell.textlabel?.text = fruits[indexpath.row]         return cell     }     @ibaction func makeselection(_ sender: any) {         tableview.allowsmultipleselectionduringediting = true         tableview.setediting(true, animated: true)     }     func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) {          let item = fruits[indexpath.row]         if let index = newfruitlist.index(of: item) {             newfruitlist.remove(at: index)         } else {             newfruitlist.append(fruits[indexpath.row])         }     }     func tableview(_ tableview: uitableview, diddeselectrowat indexpath: indexpath) {         if let index = newfruitlist.index(of: fruits[indexpath.row]) {             newfruitlist.remove(at: index)         }     } } 

how can make list items results?

fyi - reason need create list able add , remove items on table row selection otherwise stick results.

your code not same @ when not using realm. issue has nothing realm, wrongly declaring lists.

you declare them implicitly unwrapped optionals (which shouldn't in first place), try append nil list. cannot append nil, need empty list in first place. if didn't use safe unwrapping of optionals, runtime exception instead of nil result.

declare fruitselectionlist empty list , won't have issue.

var fruits: results<fruit>? var fruitselectionlist = list<fruit>()  func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) {     if let index = fruitselectionlist?.index(of: fruits[indexpath.row]) {         fruitselectionlist.remove(at: index)     } else {         fruitselectionlist.append(fruits[indexpath.row])     } } 

moreover, fruitselectionlist not managed realm, since results automatically updating, don't need put actions on fruitselectionlist inside write transactions. don't need fruitselectionlist of type list, array well.

just general piece of advice: if need selections persisted, advise update fruit model class have selected property of type bool , update property when row of tableview selected.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -