Study from the second quarter of the wavelet said Swift network programming (introductory article)
Tool: xcode6.4
First add and set three labels in Main.storyborad to do a simple interface display:
1 Import UIKit2 3 //1, construct a constructs4 structWeather {5var city:string?6var weather:string?7var temp:string?8}//why not practical class, because the structure of the initialization is convenient, do not write initialization method9 Ten One A classViewcontroller:uiviewcontroller { - -@IBOutlet weak var labelcity:uilabel! the@IBOutlet weak var labelweather:uilabel! -@IBOutlet weak var labeltemp:uilabel! - - //3, next need to add a calculated attribute +var weatherdata:weather?{ - //4. If there is any change, use Swift's unique syntax + Didset { A Configview () at } - } - - //2, then consider: A startup app is to refresh the data, so a way to - func Configview () { -Labelcity.text = Self.weatherdata?. City inLabelweather.text = Self.weatherdata?. Weather -Labeltemp.text = Self.weatherdata?. Temp to } + - //4. Create a new method for getting weather data the func getweatherdata () * { $ //nsurlsessionPanax Notoginseng //<1> Resource Location Nsurl loaded URLs -Let URL = Nsurl (string:"http://api.k780.com:88/?app=weather.today&weaid=238&&appkey=10003&sign= B59bc3ef6191eb9f747dd4e83c99f2a4&format=json") theprintln (url!) + //<2>url Session configuration (URL session config): Disk cache, memory cache, system background execution. Use the default disk cache below ALet config =nsurlsessionconfiguration.defaultsessionconfiguration () the println (config) +Config.timeoutintervalforrequest =Ten//Configure time-outs, which means that users load the network within 10 seconds - //<3> establishing a session $Let session =nsurlsession (configuration:config) $ - //tasks for <4> sessions -Let task = Session.datataskwithurl (url!, Completionhandler: {(data,_, error), Voidinch the //<6> Process Data If there are no errors in the connection - ifError = =Nil {Wuyi ifLet JSON = nsjsonserialization.jsonobjectwithdata (data, options:NSJSONReadingOptions.AllowFragments, Error:nil) as?nsdictionary{ the //The following gets the data into a dictionary, and then the JSON object, directly instantiated into a custom object, this step is relatively complex, because the use of a map function -Let Weather:weather = (Json.valueforkey ("result") as?nsdictionary). map{ Wu Weather ( -City: $0["citynm"] as?String About, Weather: $0["Weather"] as?String $, Temp: $0["Temperature_curr"] as?String - ) -}! - //<8> Update interface is very slow, because the update interface is not in the main thread, so in the main thread with the new interface A Dispatch_async (Dispatch_get_main_queue (), { +()->voidinch the //<7> After you get the data, you'll see it in the View -Self.weatherdata =Weather $ }) the the the } the } -})//The second parameter is the action to be done after the task is completed in //<5> Perform Tasks the Task.resume () the About the } the Overridefunc viewdidload () { the super.viewdidload () + Getweatherdata () - } the Bayi Overridefunc didreceivememorywarning () { the super.didreceivememorywarning () the } - - the}
Then you can run it:
Swift network programming Getting Started application: Weather forecast