iphone的基本预测应用

时间:2015-07-05 20:15:42

标签: ios swift

我正在为iPhone构建一个基本天气应用程序,我是编程新手,应用程序给我一条消息而不是显示天气,这是我默认编写的错误消息,我不知道它有什么问题,这是代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet var userCity: UITextField!

@IBAction func findWeather(sender: AnyObject) {

    var url = NSURL(string: "http://www.weather-forecast.com/locations/" + userCity.text + "/forecasts/latest")

    if url != nil{

        let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in

            if error == nil
            {
                var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding) as NSString!

                var urlContentArray = urlContent.componentsSeparatedByString("<span class=\"phrase\">")

                if urlContentArray.count > 0 {

                    var weatherArray = urlContentArray[1].componentsSeparatedByString("</span>")

                    var weather = weatherArray[0] as! String

                    self.resultLabel.text = weather

                }else{
                    self.resultLabel.text = "Was not able to find " + self.userCity.text + "weather. please try again1"
                }   
            }
            else{

            }})
        resultLabel.text = "was not able to find weather " + userCity.text + ". Please try again2"

    }
    else{
        resultLabel.text = "Was not able to find weather" + userCity.text + ". Please try again3"

0 个答案:

没有答案