Synchronous get Mode
1 //MARK:-Synchronous Get Mode2 func synchronousget ()3 {4 //Create a Nsurl object5var url:nsurl! = Nsurl (string:"http://m.weather.com.cn/data/101010100.html")6 7 //Create Request Object8var urlrequest:nsurlrequest = nsurlrequest (Url:url, CachePolicy:NSURLRequestCachePolicy.UseProtocolCachePolicy, timeOutInterval:Ten)9 Ten //Response Object Onevar response:nsurlresponse? A - //Error Object -var error:nserror? the - //make a request -var data:nsdata? = Nsurlconnection.sendsynchronousrequest (URLRequest, Returningresponse: &response, Error: &error) - + if(Error! =Nil) - { +println (error?). Code) Aprintln (error?). Description) at}Else - { -var jsonstring = NSString (data:data!, encoding:nsutf8stringencoding) - - println (jsonstring) - } in -}
iOS Development--Network Programming Swift & (iii) synchronous get mode