首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。