ios - How to add Error handling to a working tableviewcontroller with Json (Swift 2) -


im starting programming apps in xcode 7 / swift 2.0 im pretty far developing ideas, can't seem error handling work. viewcontroller concerns presenting dates , when our band plays.

in viewcontroller call json data our online server , parse tableview. works. want following things happen too.

  1. if there no connection whatsoever (wifi/4g/3g) perform segue (no connection)
  2. if server or php script unreachable, perform segue (server error )
  3. if there no data available (as in empty array) give message "there no dates set."

the json php script:

(         {         date = "some date";         description = "some description";         location = "some location";         others = "some details";         showtime = "some time";     },         {         date = "some date";         description = "some description";         location = "some location";         others = "some details";         showtime = "some time";     } ) 

this viewcontroller

import uikit  class gigsviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource {      @iboutlet weak var tableview: uitableview!      var gigsdata: nsarray = []      override func viewdidload() {         super.viewdidload()          let logo = uiimage(named: "where_is_header_navigationcontroller.jpg")         let imageview = uiimageview(image:logo)         self.navigationitem.titleview = imageview          func dataofjson(url: string) -> nsarray {             let gigsdata = nsdata(contentsofurl: nsurl(string: url)!)             let jsonarray: nsarray = try! nsjsonserialization.jsonobjectwithdata(gigsdata!, options: .mutablecontainers) as! nsarray             return jsonarray         }         gigsdata = dataofjson("http://www.mydomain.eu/app/myscript.php")      }// end of viewdidload      // mark: table view delegate methods     func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {          if gigsdata.count != 0 {             return gigsdata.count          } else {              return 1         }      }      func allowmultiplelines(tableviewcell:uitableviewcell) {         tableviewcell.textlabel?.numberoflines = 0         tableviewcell.textlabel?.linebreakmode = nslinebreakmode.bywordwrapping     }      func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {         let cell = tableview.dequeuereusablecellwithidentifier("gigscell")! uitableviewcell         // setting text color         cell.textlabel?.textcolor = uicolor.whitecolor()         //getting json data , turn objects         let maingigsdata = (gigsdata[indexpath.row] as! nsdictionary)         //setting constants detailview         let gigsdate = maingigsdata["date"] as! string         let gigslocation = maingigsdata["location"] as! string         // setting number of lines per row         cell.textlabel!.numberoflines = 2         // filling cell data         cell.textlabel!.text = ("\(gigsdate) \n\(gigslocation)")         // setting beackground color when selected         let backgroundview = uiview()         backgroundview.backgroundcolor = uicolor.darkgraycolor()         cell.selectedbackgroundview = backgroundview          return cell     }       override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }   } 

like said, im new this, please dont go around , name kinds of proceduresm, functions or things that. please don't think don't try myself, 'm stuck 2 weeks.

the thing saw lot on videos , tutorials try catch thing. implementing can gave me kinds of errors, must doing wrong there.

i hope there out there can me out , make me lot wiser today!

you should use nsurlrequest or lib alamofire fetch data. nsdata contentsofurl not asynchronous , in case blocks main thread. wonder if can compile code nsdata contentofurl throws , exception, must catched. check domain of nserror object, thrown. can e.g. nsurlerrordnslookupfailed, nsurlerrordomain, nsurlerrornotconnectedtointernet, nsurlerrorinternationalroamingoff.

https://github.com/alamofire/alamofire

to detech connection type:

detect carrier connection type (3g / edge / gprs)

example of error handling based on code:

do {     // blocks ui if in main thread!     let gigsdata = nsdata(contentsofurl: nsurl(string: url)!) } catch let error as! nserror {     if error.domain == nsurlerrornotconnectedtointernet {         // if data fetch using background queue must dispatched main queue:         performseguewithidentifier("nointernet", sender: self)     } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -