nitrogenhurricane
nitrogenhurricane

Reputation: 301

How to use CoreLocation with swift?

I've tried using CoreLocation with my Swift app, but without luck. I have the following incredibly simple code:

(I've linked the CoreLocation library.)

import UIKit
import CoreLocation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

    var window: UIWindow?
    var locationManager: CLLocationManager = CLLocationManager()
    locationManager.delegate = self

On the last line I get the error "Expected declaration."

What am I doing wrong?

Thanks in advance!

Upvotes: 3

Views: 4809

Answers (1)

ColinE
ColinE

Reputation: 70122

The following code locationManager.delegate = self is a statement. You need to execute it within one of the AppDelegate methods. For example:

class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

    var window: UIWindow?
    var locationManager: CLLocationManager = CLLocationManager()

    func application(application :UIApplication, didFinishLaunchingWithOptions launchOptions:NSDictionary>) -> Bool {
       locationManager.delegate = self
    }
}

Upvotes: 6

Related Questions