如何用swift使用CoreLocation?

我试过用我的Swift应用程序使用CoreLocation,但没有运气。 我有以下令人难以置信的简单的代码:

(我链接了CoreLocation库。)

import UIKit import CoreLocation @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate { var window: UIWindow? var locationManager: CLLocationManager = CLLocationManager() locationManager.delegate = self 

在最后一行,我得到错误“预期的声明”。

我究竟做错了什么?

提前致谢!

下面的代码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 } }