首页 > 移动平台 > 详细

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

时间:2014-11-07 18:51:06      阅读:297      评论:0      收藏:0      [点我收藏+]

 

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

/  ViewController.swift
//  Weather
//
//  Created by angela on 14/11/6.
//  Copyright (c) 2014年 angela. All rights reserved.
//
import UIKit
class 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

原文:http://www.cnblogs.com/iwangzheng/p/4081838.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!