samedi 25 juin 2016

weather forecast code swift

I am following an example in swift and I can't understand why the if statement with the arrow AND the else with the arrow are not running . Please answer very simple because I am very new programmer . Thanks a lot

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var cityTextField: UITextField!

    @IBOutlet weak var resultLabel: UILabel!

    @IBAction func findWeather(sender: AnyObject) {

        var successBool = false

        let attempedUrl = NSURL(string: "http://ift.tt/1yhujjU" + cityTextField.text!.stringByReplacingOccurrencesOfString(" ", withString: "-") + "/forecasts/latest")

        if let url = attempedUrl { //if 4 ----------------------------------------

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

                if let urlContent = data { //if 3 --------------------------------

                    let webContent = NSString(data: urlContent, encoding: NSUTF8StringEncoding)

                    let websiteArray = webContent!.componentsSeparatedByString("3 Day Weather Forecast Summary:</b><span class=\"read-more-small\"><span class=\"read-more-content\"> <span class=\"phrase\">")

                    if websiteArray.count > 1 { //if 2 -----------------------

                        let weatherArray = websiteArray[1].componentsSeparatedByString("</span>")

                        if weatherArray.count > 1 {  //if 1 ---------------------------

                            successBool = true
                            let summaryWeather = weatherArray[0].stringByReplacingOccurrencesOfString("&deg;", withString: "º")

                            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                                self.resultLabel.text = summaryWeather
                            }) // ===== dispatch_async


                        }//==if


                    }// if 2

                }// if3
                if successBool == false {    //<----------------------here ?
                    self.resultLabel.text = " Couldn't find the place you have entered . Please try again."
                }

            } // task

            task.resume()



        } else {//if 4   // <----------------------------------here ?

            self.resultLabel.text = " Couldn't find the place you have entered . Please try again."

        }
    }



    override func viewDidLoad() {
        super.viewDidLoad()


    }

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


}

Aucun commentaire:

Enregistrer un commentaire