Recently the landlord in Swift self-study pain and happy, this simplest weather forecast function encountered a lot of small pits, vomiting blood record code.
/Viewcontroller.swift//WeatherCreated by Angela on 14/11/6.//Copyright (c) 2014 Angela. All rights reserved.//ImportUIKitclassViewcontroller:uiviewcontroller {@IBOutlet var tv:uitextview? @IBAction func btnpressed (sender:anyobject) {println ("Button Clicked") Loadweather ()} override func Viewdidload () {super.viewdidload ()Do any additional setup after loading the view typically froma nib. Loadweather ()} override func didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources the can be recreated. } func Loadweather () {var URL= Nsurl (string:"http://www.weather.com.cn/data/sk/101010100.html") println ("\ (URL)") var data=NSData (contentsofurl:url!) println ("\ (data)") var json:anyobject? =nsjsonserialization.jsonobjectwithdata (data! options:NSJSONReadingOptions.AllowFragments Error:nil) var W eatherinfo:anyobject! = JSON?. Objectforkey ("Weatherinfo") var city:anyobject! = Weatherinfo?. Objectforkey (" City") var temp:anyobject! = Weatherinfo?. Objectforkey ("Temp") var wind:anyobject! = Weatherinfo?. Objectforkey ("WD") var ws:anyobject! = Weatherinfo?. Objectforkey ("WS") TV?. Text="cities: \ (city) \ n temp: \ (temp) \ n: \ (wind) \ n \ Wind: \ (WS)" }}
Swift implements the weather forecast app Weather-forecast app using Swift