如何在swift中使用CoreLocation?

nit*_*ane 3 ios swift

我已经尝试过将CoreLocation与我的Swift应用程序一起使用,但没有运气.我有以下非常简单的代码:

(我已经链接了CoreLocation库.)

import UIKit
import CoreLocation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

    var window: UIWindow?
    var locationManager: CLLocationManager = CLLocationManager()
    locationManager.delegate = self
Run Code Online (Sandbox Code Playgroud)

在最后一行,我收到错误"预期声明".

我究竟做错了什么?

提前致谢!

Col*_*inE 6

以下代码locationManager.delegate = self是一个声明.您需要在其中一个AppDelegate方法中执行它.例如:

class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

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

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