一个简单的天气APP,基于swift版本的

一个简单的天气app,基于swift版本的,其实比较简单,就不多说了,贴上代码。swift版本天气APP很easy,如果你能看懂就看,看不懂,说明你基础还需要学习,继续学习。


//
//  ViewController.swift
//  weather2
//
//  Created by jincon on 15/4/5.
//  Copyright (c) 2015年 jincon. All rights reserved.
//

import UIKit

struct Weather {
    var city: String?
    var weather: String?
    var temp: String?
    var image: String?
}

class ViewController: UIViewController {

    @IBOutlet weak var lableCity: UILabel!
    @IBOutlet weak var lableWeather: UILabel!
    @IBOutlet weak var lableTemp: UILabel!
    @IBOutlet weak var lableImage: UIImageView!
    
    var  weatherData:Weather? {
        didSet {
            configView()
        }
    }
    
    func configView(){
        lableCity.text = self.weatherData?.city
        lableWeather.text = self.weatherData?.weather
        lableTemp.text = self.weatherData?.temp
        
       
        let url2 = self.weatherData?.image
        let url = NSURL(string: url2!)
        let nsd = NSData(contentsOfURL: url!)
        
        self.lableImage.image = UIImage(data: nsd!)
        
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let url = NSURL(string: "http://api.k780.com:88/?app=weather.today&weaid=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json")
        
        let config = NSURLSessionConfiguration.defaultSessionConfiguration()
        config.timeoutIntervalForRequest = 10
        
        //建立回话
        let session = NSURLSession(configuration: config)
        
        //回话的任务
        let task = session.dataTaskWithURL(url!, completionHandler: { (data, _ , error) -> Void in
            
           
            
            if error == nil {
                
                if let json = NSJSONSerialization.JSONObjectWithData(data , options: NSJSONReadingOptions.AllowFragments, error: nil ) as? NSDictionary {
                    
                    //println(json)
                    
                    let weather = { json.valueForKey("result").map{
                        Weather(city:$0["citynm"] as? String, weather:$0["weather"] as? String, temp:$0["temperature_curr"] as? String,image:$0["weather_icon"] as? String )
                            //
                        }
                    }
                    //println(weather)
                    
                    dispatch_async(dispatch_get_main_queue(),{ () -> Void in
                    self.weatherData = weather()
                    
                    })
                    
                }
                
            }
        })
        
        //执行任务
        task.resume()
        
        
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

关键词: swift编程 , 天气app

上一篇: Object-C与Swift的编程语言区别对比(二)
下一篇: swift扩展SwiftyJSON 中文介绍和使用说明

目前还没有人评论,您发表点看法?
发表评论

评论内容 (必填):