Trong quá trình tìm hiểu để có được vị trí của người dùng trong một lớp đơn, tôi đã tải xuống mã này https://github.com/irfanlone/CLLocationManager-Singleton-Swift. Sau khi foddleing với nó để làm cho chạy trên Xcode8.3.3 Swift 3, tôi không thể nhận được bất kỳ đầu ra trong giao diện điều khiển gỡ lỗi. Trong thực tế, tôi thậm chí không thể có được vị trí này tự động hóa dlalog để hiển thị. Tôi nghi ngờ các dữ liệu từ singleton không được thông qua để xem bộ điều khiển, nhưng tôi không thể phạt các giải pháp, bạn có thể xem những gì là sai? Cảm ơn.
Mã chỉnh cho Swift 3 là: // Các singleton: nhập khẩu MapKit
giao thức LocationUpdateProtocol { func locationDidUpdateToLocation (_ vị trí: CLLocation) }
hãy kLocationDidChangeNotification = "LocationDidChangeNotification"
lớp UserLocationManager: NSObject, CLLocationManagerDelegate {
static let SharedManager = UserLocationManager()
fileprivate var locationManager = CLLocationManager()
var currentLocation : CLLocation?
var delegate : LocationUpdateProtocol!
fileprivate override init() {
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLLocationAccuracyHundredMeters
locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}
// MARK: - CLLocationManagerDelegate
func locationManager(manager: CLLocationManager,didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
currentLocation = newLocation
let userInfo : NSDictionary = ["location" : currentLocation!]
DispatchQueue.main.async() {() -> Void in
self.delegate.locationDidUpdateToLocation(self.currentLocation!)
NotificationCenter.default.post(name: Notification.Name(kLocationDidChangeNotification), object: self, userInfo: userInfo as [NSObject : AnyObject])
}
}
}
// Các ViewController nhập khẩu UIKit nhập khẩu CoreLocation
lớp ViewController: UIViewController, LocationUpdateProtocol {
var currentLocation : CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.locationUpdateNotification(_:)), name: NSNotification.Name(rawValue: kLocationDidChangeNotification), object: nil)
let LocationMgr = UserLocationManager.SharedManager
LocationMgr.delegate = self
}
// MARK: - Notifications
func locationUpdateNotification(_ notification: Notification) {
let userinfo = notification.userInfo
self.currentLocation = userinfo!["location"] as! CLLocation
print("Latitude : \(self.currentLocation.coordinate.latitude)")
print("Longitude : \(self.currentLocation.coordinate.longitude)")
}
// MARK: - LocationUpdateProtocol
func locationDidUpdateToLocation(_ location: CLLocation) {
currentLocation = location
print("Latitude : \(self.currentLocation.coordinate.latitude)")
print("Longitude : \(self.currentLocation.coordinate.longitude)")
}
}
'locationManager: didUpdateToLocation: fromLocation' là một phương thức đại biểu. Trình quản lý vị trí sử dụng phương pháp này để thông báo cho đối tượng của bạn rằng vị trí đã được cập nhật với một giá trị mới - tùy thuộc vào bạn để thực hiện phương pháp này để làm điều gì đó với giá trị. – quellish