使用局部变量""在声明错误之前

时间:2016-07-21 19:48:33

标签: ios swift

在使用" locationManager"在Xcode中设置GPS时出现上述错误。不知道该怎么办。 - 编辑 - 我清理了文件并修复了错误,直到我在viewDidLoad之外添加了func locationManager行,并且错误返回到了locationManager行。

代码:

class ViewController: UIViewController, CLLocationManagerDelegate {

    // labels

    @IBOutlet var cityLabel: UILabel!
    @IBOutlet var tempratureLabel: UILabel!
    @IBOutlet var rainLabel: UILabel!
    @IBOutlet var windLabel: UILabel!
    @IBOutlet var humidityLabel: UILabel!
    @IBOutlet var backgroundColor: UIImageView!
    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        // USER GPS IN LAT/LON

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

        // api
        let client = APIClient(apiKey: "xxxxxxxxxxxxxxxxxxxxxxxx")
        client.units = .Auto
        client.getForecast(latitude: -75, longitude: -26) { (currentForcast, error) -> Void in
            if let currentTemperature = currentForcast!.currently?.temperature {
                self.tempratureLabel.text = String(currentTemperature)
                let backgroundTemp = currentForcast!.currently?.temperature
                //update background image per temp
                if backgroundTemp <= 9  {
                    self.backgroundColor.image = UIImage(named: "Gradient1_Iphone6-GREY.png")
                } else if backgroundTemp >= 10 && backgroundTemp <= 19 {
                    self.backgroundColor.image = UIImage(named: "Gradient1_Iphone6-DARKPURP.png")
                } else if backgroundTemp >= 20 && backgroundTemp <= 29 {
                    self.backgroundColor.image = UIImage(named: "Gradient1_Iphone6-DARKBLUE.png")
                } else if backgroundTemp >= 30 && backgroundTemp <= 39 {
                    self.backgroundColor.image = UIImage(named: "Gradient1_Iphone6-DGREEN.png")
                } else if backgroundTemp >= 40 && backgroundTemp <= 49 {
                    self.backgroundColor.image = UIImage(named: "Gradient1_Iphone6-MIDPURP.png")
                } else if backgroundTemp >= 50 && backgroundTemp <= 59 {
                    self.backgroundColor.image = UIImage(named: "Gradient1_Iphone6-MIDBLUE.png")
                } else if backgroundTemp >= 60 && backgroundTemp <= 69 {
                    self.backgroundColor.image = UIImage(named: "Gradient1_Iphone6-LIGHTPURP.png")
                } else if backgroundTemp >= 70 && backgroundTemp <= 75 {
                    self.backgroundColor.image = UIImage(named: "Gradient1_Iphone6-LIGHTBLUE.png")
                } else if backgroundTemp >= 76 && backgroundTemp <= 78 {
                    self.backgroundColor.image = UIImage(named: "Gradient1_Iphone6-LGREEN.png")
                } else if backgroundTemp >= 79 && backgroundTemp <= 81 {
                    self.backgroundColor.image = UIImage(named: "Gradient1_Iphone6_YELLOW.png")
                } else if backgroundTemp >= 82  && backgroundTemp <= 87 {
                    self.backgroundColor.image = UIImage(named: "Gradient1_Iphone6_ORANGE.png")
                } else if backgroundTemp >= 88  && backgroundTemp <= 98 {
                    self.backgroundColor.image = UIImage(named: "Gradient1_Iphone6-LIGHTRED.png")
                } else {
                    self.backgroundColor.image = UIImage(named: "Gradient1_Iphone6-DARKRED.png")
                }
            }

            //update minor labels
            let rain = 10
            self.rainLabel.text = String(rain) + " %"
            let humidity = 10
            self.humidityLabel.text = String(humidity) + " %"
            let wind = 10
            self.windLabel.text = String(wind) + " mph"

            // Do any additional setup after loading the view, typically
        }

        func locationManager (manager: CLLocationManager! , didUpdateLocation locations: [AnyObject]!) {
            print(locations)
        }

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

4 个答案:

答案 0 :(得分:8)

我不知道这是否有用,但是我搞砸了函数的开始和结束括号。 在尝试自己调试之前尝试使用Google搜索。傻傻的我!

答案 1 :(得分:3)

供将来参考:

当您遇到像

这样的奇怪错误时
  

在声明错误之前使用局部变量“”

你应该意识到它显然没有识别你输入的内容(假设“”不是变量名称)。

解决方案通常是良好的'开启 - 关闭(重启Xcode)或干净的CMD + Shift + K并构建CMD + B

另外,你添加了

func locationManager (manager: CLLocationManager! , didUpdateLocation locations: [AnyObject]!) {

viewDidLoad内。你需要把它移到外面。

答案 2 :(得分:1)

class cat {
  var legs? = 4
  func manuallyCountLegs() -> Int {
    guard legs ?? 0 < 50 else {
      // Bail, I don't have time for this much counting
      return legs ?? 0
    }
    let legs = self.legs
    ...
  } 
}

这样做会失败,因为它顶部的腿(如果它引用self.legs则非常好)是打算使用下面的本地范围的legs

答案 3 :(得分:0)

只需添加self。“变量名”或添加函数名即可。