【发布时间】:2014-07-26 15:49:38
【问题描述】:
我正在尝试将 ObjC 中的旧应用程序转换为 Swift 作为练习,但遇到了一些问题。我在旧应用程序中使用它的方式是建立 CLLocation 管理器,然后我会使用:
manager = [[CLLocationManager alloc]init];
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
[manager startUpdatingLocation]
自动调用:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
}
从那里我可以提取我需要的所有信息。但是很快,这种方法没有自动完成功能,我无法弄清楚如何重现它。文档说
startUpdatingLocation()
仍然会被委托调用,但它没有发生。
这是我目前所拥有的:
import UIKit
import corelocation
class ViewController: UIViewController,CLLocationManagerDelegate{
@IBOutlet var gpsResult : UILabel
var manager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
}
func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[]) {
println("locations = \(locations)")
gpsResult.text = "success"
}
}
任何有关查看位置的帮助或指示将不胜感激。谢谢。
编辑:根据建议更新,但仍然无法正常工作
EDIT2:似乎是一些错误不允许该方法在 ViewController 中正常工作
【问题讨论】:
-
你可以在这里看到完整的答案:stackoverflow.com/a/31277543/3166417
-
edit 2 还有效吗?既然你接受了答案..