ios - Load data from a plist to two TableViews -


i having trouble load data property list. take look… directory.plist: directory.plist

i wanna show of this, right here on main.storyboard: main.storyboard

but keys position e name appear on first tableviewcontroller , keys functionary, imageface , phone have appear on second tableviewcontroller.

to this, made this:

  1. added appdelegate following:

    func application(_ application: uiapplication, didfinishlaunchingwithoptions launchoptions: [uiapplicationlaunchoptionskey: any]?) -> bool {     if let url = bundle.main.url(forresource: "directory", withextension: "plist"), let array = nsarray(contentsof: url) as? [[string:any]] {         shared.instance.employees = array.map{employee(dictionary: $0)} } return true 
  2. created struct this:

    struct employeedetails {     let functionary: string     let imageface: string     let phone: string      init(dictionary: [string: any]) {         self.functionary = (dictionary["functionary"] as? string) ?? ""         self.imageface = (dictionary["imageface"] as? string) ?? ""         self.phone = (dictionary["phone"] as? string) ?? ""     } }  struct employee {     let position: string     let name: string     let details: [employeedetails] // [string:any]      init(dictionary: [string: any]) {     self.position = (dictionary["position"] as? string) ?? ""     self.name = (dictionary["name"] as? string) ?? ""      let t = (dictionary["details"] as? [any]) ?? []     self.details = t.map({employeedetails(dictionary: $0 as! [string : any])})     } }  struct shared {     static var instance = shared()     var employees: [employee] = [] } 
  3. my first tableviewcontroller, this:

    class page1: uitableviewcontroller {  override func viewdidload() {     super.viewdidload()      let anemployee = shared.instance.employees[1]      print("name:", anemployee.name)     print("position:", anemployee.position)      anemployee.details.foreach({          print("functionary:", $0.functionary)         print("imageface:", $0.imageface)         print("phone:", $0.phone)     }) }  override func numberofsections(in tableview: uitableview) -> int { return 1 }  override func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {     return shared.instance.employees.count }  override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecell(withidentifier: "cell", for: indexpath) as! tableviewcell1          cell.namelabel.text = shared.instance.employees[indexpath.row].name         cell.positionlabel.text = shared.instance.employees[indexpath.row].position          return cell     }  override func prepare(for segue: uistoryboardsegue, sender: any?) {     if let destination = segue.destination as? page2,         let indexpath = tableview.indexpathforselectedrow {         destination.newpage = shared.instance.employees[indexpath.row].details[indexpath.row]         tableview .deselectrow(at: indexpath, animated: true)         }     } } 
  4. my second tableviewcontroller, here:

    var newpage: employeedetails! //recently added  class page2: uitableviewcontroller {  var newpage: employeedetails!  override func viewdidload() {     super.viewdidload()      if let theemployee = newpage {         self.title = theemployee.name     } }  override func numberofsections(in tableview: uitableview) -> int {     return 1 }  override func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {     if let theemployee = newpage {         return theemployee.details.count     } return 0 }  override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecell(withidentifier: "cell", for: indexpath) as! tableviewcell2      if let theemployee = newpage {          cell.faceimage.image = theemployee.details[indexpath.row].imageface as? uiimage // did not work         cell.functionary.text = theemployee.details[indexpath.row].functionary         cell.phonelabel.text = theemployee.details[indexpath.row].phone     }     return cell     } } 

when touch in item of first tableviewcontroller, see second tableviewcontroller totally empty! , debug area shows it:

2017-03-28 17:16:28.456 plist sample[7138:425253] unknown class page2 in interface builder file. 

according plist structure:

shared.instance.employees[indexpath.row].details 

details array of dictionaries. treating dictionary.

edit: initial issue correct... multiple ways solve (as have seen / done). option, may or may not helpful:

struct employeedetails {     let functionary: string     let imageface: string     let phone: string      init(dictionary: [string: any]) {         self.functionary = (dictionary["functionary"] as? string) ?? ""         self.imageface = (dictionary["imageface"] as? string) ?? ""         self.phone = (dictionary["phone"] as? string) ?? ""     } } struct employee {     let position: string     let name: string     let details: [employeedetails] // [string:any]      init(dictionary: [string: any]) {         self.position = (dictionary["position"] as? string) ?? ""         self.name = (dictionary["name"] as? string) ?? ""          let t = (dictionary["details"] as? [any]) ?? []         self.details = t.map({employeedetails(dictionary: $0 as! [string : any])})     } }  struct shared {     static var instance = shared()     var employees: [employee] = [] } 

you can original:

    if let url = bundle.main.url(forresource: "directory", withextension: "plist"), let array = nsarray(contentsof: url) as? [[string:any]] {          shared.instance.employees = array.map{employee(dictionary: $0)}      } 

and access data via:

    let anemployee = shared.instance.employees[0]      print("name:", anemployee.name)     print("position:", anemployee.position)      print("detail 0 functionary:", anemployee.details[0].functionary)     print("detail 0 imageface:", anemployee.details[0].imageface)     print("detail 0 phone:", anemployee.details[0].phone)      // or      anemployee.details.foreach({          print("functionary:", $0.functionary)         print("imageface:", $0.imageface)         print("phone:", $0.phone)      }) 

just example.

see https://github.com/donmag/swplistdata working example.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -