Making API call using swift -


i total noob when comes ios coding. im trying learn how make api call "http://de-coding-test.s3.amazonaws.com/books.json" however, since i'm total noob, tutorials find make no sense how it. want learn how can json data web , input uitableviewcell

i have looked through 3 dozen tutorials, , nothing makes sense.

any appreciated.

let's going step:

1) framework you're going use make api call nsurlsession (or library alomofire, etc).

an example make api call:

func getbooksdata(){      let url = "http://de-coding-test.s3.amazonaws.com/books.json"      (nsurlsession.sharedsession().datataskwithurl(nsurl(string: url)!) { (data:nsdata?, response:nsurlresponse?, error:nserror?) -> void in         //here we're converting json nsarray         if let jsondata = (try? nsjsonserialization.jsonobjectwithdata(data!, options: nsjsonreadingoptions.mutableleaves)) as? nsarray{             //create local variable save data receive             var results:[book] = []             bookdict in jsondata bookdict nsdictionary{                 //create book objects , add array of results                 let book = book.objectwithdictionary(bookdict as! nsdictionary)                 results.append(book)             }             dispatch_async(dispatch_get_main_queue(), { () -> void in                 //make call ui on main queue                 self.books = results                 self.tblbooks.reloaddata()             })         }     }).resume() } 

book entity:

class book{     var title:string      init(title:string){         self.title = title     }      class func objectwithdictionary(dict:nsdictionary)->book{         var title = ""         if let titletmp = dict.objectforkey("title") as? string{             title = titletmp         }         return book(title: title)     }    } 

note: in practice, check error , status code of response, , can extract code of making api call class (or service layer).one option, using pattern of datamapper, can create class manager entities (in example book bookmanager) , can make (you can abstract more, creating general api, receive url , return anyobject transformation of json, , there process inside manager):

class bookmanager{      let sharedinstance:bookmanager = bookmanager()      private init(){}      func getbookdata(success:([book])->void,failure:(string)->void){         let url = "http://de-coding-test.s3.amazonaws.com/books.json"          (nsurlsession.sharedsession().datataskwithurl(nsurl(string: url)!) { (data:nsdata?, response:nsurlresponse?, error:nserror?) -> void in              if error != nil{                 failure(error!.localizeddescription)             }else{                 if let jsondata = (try? nsjsonserialization.jsonobjectwithdata(data!, options: nsjsonreadingoptions.mutableleaves)) as? nsarray{                     var results:[book] = []                     bookdict in jsondata bookdict nsdictionary{                         let book = book.objectwithdictionary(bookdict as! nsdictionary)                         results.append(book)                     }                    success(results)                 }else{                     failure("error format")                 }             }         }).resume()      }  } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -