如何在swift中使用CoreLocation?

时间:2014-06-09 13:53:20

标签: 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

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

我做错了什么?

提前致谢!

1 个答案:

答案 0 :(得分: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
    }
}