简体   繁体   English

Swift-使用无法解析的标识符

[英]Swift - Use of unresolved identifier

I create all the variables within the same function. 我在同一函数中创建所有变量。 But at the end of the function I can't reach the constant even though it was created in the same function. 但是在函数的最后,即使它是在同一函数中创建的,我也无法达到该常数。

When I write "self.resultLabel.text = weather" at the end, Xcode shows me an error use of unresolved identifier 'weather' 当我最后写“ self.resultLabel.text = weather”时,Xcode向我显示了一个错误的未解析标识符“ weather”的使用

I know how to fix it. 我知道如何解决。 I need to initiate 'weather' just after the task method starts and set it to "" and then I should change its value in the function but even if I don't do it and I create it in the if closures, shouldn't I be able to reach it while within the same function? 我需要在task方法启动后立即启动“天气”并将其设置为“”,然后我应该在函数中更改其值,但是即使我不这样做并且我在if闭包中创建它,也不应我可以在相同功能范围内达到目标吗?

I don't understand why it keeps me giving this error. 我不明白为什么它会一直给我这个错误。

func findWeather() {

        var errorStatus = false

        city = (cityField.text?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()))!

        let url = NSURL(string: "http://www.weather-forecast.com/locations/" + city.stringByReplacingOccurrencesOfString(" ", withString: "-") + "/forecasts/latest")!

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

            if let content = data {

                let urlContent = NSString(data: content, encoding: NSUTF8StringEncoding) as NSString!
                let weatherContent = urlContent.componentsSeparatedByString("<span class=\"phrase\">")
                if weatherContent.count > 1 {

                    let weather = weatherContent[1].componentsSeparatedByString("</span>")[0].stringByReplacingOccurrencesOfString("&deg;", withString: "˚")


                } else {

                    var weather = ""
                    errorStatus = true

                }


            } else {

                var weather = ""
                errorStatus = true

            }


            dispatch_async(dispatch_get_main_queue(), { () -> Void in

                if errorStatus == true {

                    self.showError()

                } else {

                    self.resultLabel.text = weather // I get error: use of unresolved identifier 'weather'

                }

            })

        }

        task?.resume()

You're defining too many new variables weather which are all visible only locally between the curly braces they are in. 您正在定义太多新的变量weather ,这些变量只能在它们所在的花括号之间局部可见。

The best place to define weather is at the beginning of the task closure and all var or let declarations of the subsequent occurrences of the variable have to be deleted. 定义weather的最佳位置是在task关闭的开始,并且必须删除该变量随后出现的所有varlet声明。

Here the crucial part: 这里是关键部分:

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

    var weather = ""
    if let content = data {
      let urlContent = NSString(data: content, encoding: NSUTF8StringEncoding) as NSString!
      let weatherContent = urlContent.componentsSeparatedByString("<span class=\"phrase\">")
      if weatherContent.count > 1 {
        weather = weatherContent[1].componentsSeparatedByString("</span>")[0].stringByReplacingOccurrencesOfString("&deg;", withString: "˚")
      } else {
        errorStatus = true
      }
    } else {
      errorStatus = true
    }

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM