ios - Need guidance about NSUserDefaults -


i no errors think i'm missing something. of code done i'm missing loading on viewdidload, , if i'm saving correctly. tried doing saving 2 arrays (testarray , followedarray) not working far. custom objects difficult.

don't need code guidance on i'm missing working.

the point of app when tableview loads, gets json , displays in section 1, each row has button , when clicked moves row section 0 (also section 1 if clicked again). i'm trying save rows position nsuserdefaults because don't need save json info user left row. in saving rows position, app stream json info ever position in.

also, have search controller in app search through items in section 1 not section 0.

section 0 = clicked items (followedarray)

section 1 = unclicked items (testarray)

when test on device, nsuserdefaults doesn't load when app starts when click followbutton not doing anything. not loading correctly or saving or both?

hopefully can guide me here, thank you.

viewcontroller.swift

var jsonarray: nsmutablearray = [] var testarray = [test]() var followedarray = [test]() var filteredarray = [test]()  // nsuserdefaults var data: [any]? var items: [[any]]?  // title header func tableview(_ tableview: uitableview, titleforheaderinsection section: int) -> string? {      if !(searchcontroller.isactive && searchcontroller.searchbar.text != "") {          if section == 0 {             return "followed items"         }         else {             return "all items"         }     }      return "filtered items" }  // number of rows in section func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {      if !(searchcontroller.isactive && searchcontroller.searchbar.text != "") {          if section == 0 {              return followedarray.count         }         else if (section == 1) {              return testarray.count         }      }     return filteredarray.count }  // number of sections func numberofsections(in tableview: uitableview) -> int {      if !(searchcontroller.isactive && searchcontroller.searchbar.text != "") {          return 2     }     return 1 }  // follow button @ibaction func followbuttonclick(_ sender: uibutton!) {      // adding row tag     let buttonposition = (sender anyobject).convert(cgpoint.zero, to: self.mytableview)     if let indexpath = self.mytableview.indexpathforrow(at: buttonposition) {          // change follow following         (sender uibutton).setimage(uiimage(named: "follow.png")!, for: .normal)         cell.followbutton.ishidden = true         cell.followedbutton.ishidden = false          // checking wether import testarray or filteredarray followedarray         if !(searchcontroller.isactive && searchcontroller.searchbar.text != "") {              self.mytableview.beginupdates()              // ----- inserting cell followedarray -----             followedarray.insert(testarray[indexpath.row], at: 0)             mytableview.insertrows(at: [indexpath(row: 0, section: 0)], with: .fade)              // ----- removing cell testarray -----             testarray.remove(at: indexpath.row)             let rowtoremove = indexpath.row             self.mytableview.deleterows(at: [indexpath(row: rowtoremove, section: 1)], with: .fade)              self.mytableview.endupdates()              // nsuserdefaults             savesorting() { "\($0)" }              mytableview.reloaddata()         }         else {              self.mytableview.beginupdates()              // ----- inserting cell followedarray -----             let testobject: test = filteredarray[indexpath.row]             let indexofobjectinarray = testarray.index(of: testobject)              followedarray.insert(testobject, at: 0)              // ----- removing cell filteredarray -----             filteredarray.remove(at: indexpath.row)             testarray.remove(at: indexofobjectinarray!)             let rowtoremove = indexpath.row             self.mytableview.deleterows(at: [indexpath(row: rowtoremove, section: 0)], with: .fade)              self.mytableview.endupdates()              // nsuserdefaults             savesorting() { "\($0)" }              mytableview.reloaddata()         }     } }  // nsuserdefaults func fetchdata() {      // request remote or local     data = [testarray]      // update items first section has 0 elements,     // , place data in section 1     items = [[], data ?? []]      // apply ordering     applysorting() { "\($0)" }      // save ordering     savesorting() { "\($0)" }      // refresh table view     mytableview.reloaddata() }  func applysorting(_ dataidblock: (any) -> string) {      // saved ordering     guard let data = self.data else { return }     let ordering = datahandling.allsavedordering(data.count)      var result: [[any]] = [[], []]      (section, ordering) in ordering {         guard section <= 1 else { continue } // make sure section 0 or 1         let rows = data.filter({ obj -> bool in             return ordering.index(where: { $0.dataid == .some(dataidblock(obj)) }) != nil         })         result[section] = rows     }      self.items = result }  func savesorting(_ dataidblock: (any) -> string) {      guard let items = self.items else { return }      (section, rows) in items.enumerated() {         (row, item) in rows.enumerated() {             let indexpath = indexpath(row: row, section: section)             let dataid = dataidblock(item)             let ordering = datahandling(dataid: dataid, indexpath: indexpath)             ordering.save(defaults: indexpath.defaultskey)         }     }  }  }  extension indexpath { var defaultskey: string { return "data_ordering_\(section)_\(row)" } } 

datahandling.swift

class datahandling: nsobject, nscoding {  var indexpath: indexpath? var dataid: string?  init(dataid: string, indexpath: indexpath) {     super.init()     self.dataid = dataid     self.indexpath = indexpath }  required init(coder adecoder: nscoder) {      if let dataid = adecoder.decodeobject(forkey: "dataid") as? string {         self.dataid = dataid     }      if let indexpath = adecoder.decodeobject(forkey: "indexpath") as? indexpath {         self.indexpath = indexpath     }  }  func encode(with acoder: nscoder) {     acoder.encode(dataid, forkey: "dataid")     acoder.encode(indexpath, forkey: "indexpath") }  func save(defaults key: string) -> bool {      let defaults = userdefaults.standard     let saveddata = nskeyedarchiver.archiveddata(withrootobject: self)     defaults.set(saveddata, forkey: key)     return defaults.synchronize()  }  convenience init?(defaults key: string) {      let defaults = userdefaults.standard     if let data = defaults.object(forkey: key) as? data,         let obj = nskeyedunarchiver.unarchiveobject(with: data) as? datahandling,         let dataid = obj.dataid,         let indexpath = obj.indexpath {         self.init(dataid: dataid, indexpath: indexpath)     } else {         return nil     }  }  class func allsavedordering(_ maxrows: int) -> [int: [datahandling]] {      var result: [int: [datahandling]] = [:]     section in 0...1 {         var rows: [datahandling] = []         row in 0..<maxrows {             let indexpath = indexpath(row: row, section: section)             if let ordering = datahandling(defaults: indexpath.defaultskey) {                 rows.append(ordering)             }             rows.sort(by: { $0.indexpath! < $1.indexpath! })         }         result[section] = rows     }      return result   }  } 


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -