首页 > 代码库 > swift实现天气预报应用weather-forecast app Using swift

swift实现天气预报应用weather-forecast app Using swift

 

最近楼主在swift的自学中痛并快乐着,这个最简单的天气预报功能遇到了好多小坑, 吐血记录下代码.

/  ViewController.swift//  Weather////  Created by angela on 14/11/6.//  Copyright (c) 2014年 angela. All rights reserved.//import UIKitclass ViewController: 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 from a nib.        loadWeather()    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that 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 weatherInfo : 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 = "城市:\(city)\n温度: \(temp)\n风: \(wind)\n风级:\(ws)"    }}

 

swift实现天气预报应用weather-forecast app Using swift