Friday 15 August 2014

api - How to print JSON data by Mashape on Swift? -



api - How to print JSON data by Mashape on Swift? -

i tried utilize "yoda speak" api mashape in swift. print info api. succeeded calling "response" method after "getdata" method simulator out in "response" method. know there're similar questions, couldn't. please give me advices.

func response(res: nsurlresponse!, data: nsdata!, error: nserror!) { println("response") if error != nil { // if there error in web request, print console println(error.localizeddescription) } else { println("succeeded") } // simulator out here. var json: nsdictionary = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: nil) nsdictionary value in json { dispatch_async(dispatch_get_main_queue(), { () -> void in println(value) }) } } func getdata() { println("getdata") // url. allow url = nsurl(string: "https://yoda.p.mashape.com/yoda?sentence=you+will+learn+how+to+speak+like+me+someday.++oh+wait.")! // urlrequest. var req = nsmutableurlrequest(url: url) // header. req.setvalue("jy0behhcbpmsh8j1mpa5p11tcjgyp1tok3zjsn4ubbvnnp5jt3", forhttpheaderfield: "x-mashape-key") allow connection: nsurlconnection = nsurlconnection(request: req, delegate: self, startimmediately: false)! // connection server. nsurlconnection.sendasynchronousrequest(req, queue: nsoperationqueue.mainqueue(), completionhandler: self.response) println("complete") }

i give alamofire try. know works swiftyjson. there great illustration on github page on how request/receive response alamofire.

json api swift mashape

No comments:

Post a Comment