Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of local variable "" before its declaration error

Tags:

ios

swift

I get the above error when setting up GPS in Xcode at the lines with "locationManager". Not sure what to do. - edit- I cleaned the file and the error's were fixed until I added the func locationManager line outside of the viewDidLoad and the errors are back on the locationManager lines.

Code:

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.
        }
    }
}
like image 685
Lou Avatar asked Jul 21 '16 19:07

Lou


People also ask

Can use local variable before it is declared?

Using local variables. A variable that you declare inside an action is a local variable. You must declare a local variable (specifying its type) and initialize that variable before you can use it.

Where do you declare local variables?

Declaring Local Variables You can declare them at the start of the program, within the main method, inside classes, and inside methods or functions. Depending on where they are defined, other parts of your code may or may not be able to access them. A local variable is one that is declared within a method.


3 Answers

I don't know if this would be helpful, but I had messed up the opening and closing braces of the function. Tried googling it before trying to debug it myself. Silly me!

like image 189
dev Avatar answered Nov 25 '22 00:11

dev


For future reference:

When you are getting a weird error like

Use of local variable “” before its declaration error

you should realize that it's clearly not recognizing what you have typed (given that "" is not a variable name).

The solution is usually the good ol' turn-it-off-and-on (restart Xcode) or a clean CMD + Shift + K and build CMD + B.

Also, you added

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

inside of viewDidLoad. You'll need to move that outside.

like image 29
Wyetro Avatar answered Nov 24 '22 23:11

Wyetro


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
    ...
  } 
}

This fails because it things the legs up top (which is perfectly fine if it refers to self.legs) was intended to use the local-scoped legs below.

like image 41
William Entriken Avatar answered Nov 25 '22 00:11

William Entriken