ios - Parsing a JSON file(stored locally) and thus creating an array of objects in swift -


i beginner json parsing terminology. trying fetch data json file stored locally in swift project. have done things correct , can fetch data presently. but, unable create array of objects(objects created based on set of values coming json file) . , use array in view controller(companyviewcontroller.swift) class. have written json parsing code in different class(companyhandler.swift here..) , fields have been initialized in swift class(company.swift here..).

my companyviewcontroller.swift controller file following:

class companyviewcontroller: uiviewcontroller, uicollectionviewdatasource, uicollectionviewdelegateflowlayout {      @iboutlet var collectionview: uicollectionview!      override func viewdidload() {         super.viewdidload()          var customcollectionviewcell = customcollectionviewcell()          collectionview.registernib(uinib(nibname: "customcollectionviewcell", bundle:nil), forcellwithreuseidentifier: "customcollectionviewcell")         collectionview!.multipletouchenabled = true         collectionview!.backgroundcolor = uicolor.whitecolor()         self.view.addsubview(collectionview!)         appengine().getcompanydetails()      }      func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath) {         var cell = collectionview.cellforitematindexpath(indexpath) customcollectionviewcell //        cell.customimageview?.image = uiimage(named: "\(array[indexpath.row])")         appengine().getcompanydetails()  //        if ((cell.selected) && (indexpath.row == 0)) { //        var vc: abcviewcontroller = abcviewcontroller(nibname: "abcviewcontroller", bundle: nil) //        self.navigationcontroller?.pushviewcontroller(vc, animated: true) //        }     }      func collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize {         var abc = cgsize(width: collectionview.frame.size.width,height: collectionview.frame.size.height/2)         return abc     }      func collectionview(collectionview: uicollectionview, shouldhighlightitematindexpath indexpath: nsindexpath) -> bool {         return true     }      func collectionview(collectionview: uicollectionview, shouldselectitematindexpath indexpath: nsindexpath) -> bool {         return true     }      func numberofsectionsincollectionview(collectionview: uicollectionview) -> int {         return 1     }      func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int {         return 15     }      func collectionview(collectionview: uicollectionview, shouldshowmenuforitematindexpath indexpath: nsindexpath) -> bool {         return true     }      func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell {         let cell = collectionview.dequeuereusablecellwithreuseidentifier("customcollectionviewcell", forindexpath: indexpath) customcollectionviewcell          cell.customimageview?.image = uiimage(named: "image1.png")         cell.customlabel?.text = "\(indexpath.row)"         return cell     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     } }   company.swift controller file following:  class company {     var companyid: string     var companyname: string     var companyaddress: string     var companywebaddress: string     var companyfb_address: string     var companytwt_handle: string     var companyli_address: string     var aboutus: string     var email_id: string     var ivrno: string //    var projects: nsmutablearray = ["project"] //    var projectstatus: string      init(companyid: string, companyname: string,companyaddress: string, companywebaddress: string, companyfb_address: string, companytwt_handle: string, companyli_address: string, aboutus: string, email_id: string, ivrno: string)     {         self.companyid = companyid         self.companyname = companyname         self.companyaddress = companyaddress         self.companywebaddress = companywebaddress         self.companyfb_address = companyfb_address         self.companytwt_handle = companytwt_handle         self.companyli_address = companyli_address         self.aboutus = aboutus         self.email_id = email_id         self.ivrno = ivrno //        self.projects = projects //        self.projectstatus = projectstatus     } }    companyhandler.swift controller file following:  class companyhandler {  // mark: - company details     func getcompanydetails() {     var jsonpath = nsbundle.mainbundle().pathforresource("companydetails", oftype: "json")         var data = nsdata(contentsoffile: jsonpath!)         var jsondata: nsdictionary = nsjsonserialization.jsonobjectwithdata(data!,             options: nsjsonreadingoptions.mutablecontainers, error: nil) nsdictionary          var companyarray:nsarray = jsondata["companydetails"] nsarray!          in companyarray{             var cmpid: string = i["companyid"] string             var cmpname: string = i["companyname"] string             var cmpadd: string = i["companyaddress"] string             var cmpwebadd: string = i["companywebaddress"] string             var cmpfbadd: string = i["companyfb_address"] string             var cmptwtadd: string = i["companytwt_handle"] string             var cmpliadd: string = i["companyli_address"] string             var abtus: string = i["aboutus"] string             var emailid: string = i["email_id"] string             var ivrno: string = i["ivrno"] string             println("\(ivrno)")          var company = company(companyid: "cmpid", companyname: "cmpname", companyaddress: "cmpadd", companywebaddress: "cmpwebadd", companyfb_address: "cmpfbadd", companytwt_handle: "cmptwtadd", companyli_address: "cmpliadd", aboutus: "abtus", email_id: "emailid", ivrno: "ivrno")         }     } } 

you're not creating array of company objects, act data source collection view. you're initializing new company objects not doing them. create property on companyviewcontroller , make of type [company]. in each iteration through json dictionaries, after create new company, append array of company's, use array collection view.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -