एक सिंगलटन कक्षा में उपयोगकर्ता स्थान प्राप्त करने के लिए सीखने की प्रक्रिया में, मैंने इस कोड को https://github.com/irfanlone/CLLocationManager-Singleton-Swift डाउनलोड किया। Xcode8.3.3 स्विफ्ट 3 पर चलाने के लिए इसके साथ फिसलने के बाद, मुझे डीबग कंसोल में कोई आउटपुट नहीं मिल सकता है। वास्तव में मैं प्रदर्शित करने के लिए इस स्थान autherization dlalog भी नहीं प्राप्त कर सकता। मुझे संदेह है कि सिंगलटन से डेटा व्यू कंट्रोलर को पास नहीं किया गया है, लेकिन मैं समाधान ठीक नहीं कर सकता, क्या आप देख सकते हैं कि क्या गलत है? धन्यवाद।
स्विफ्ट 3 के लिए सही कोड है: // सिंगलटन: आयात MapKit
प्रोटोकॉल LocationUpdateProtocol { समारोह locationDidUpdateToLocation (_ स्थान: CLLocation) }
जाने kLocationDidChangeNotification = "LocationDidChangeNotification"
वर्ग उपयोगकर्ता स्थान प्रबंधक: एनएसओब्जेक्ट, 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])
}
}
}
// ViewController आयात UIKit आयात CoreLocation
वर्ग 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' है एक प्रतिनिधि विधि। स्थान प्रबंधक इस ऑब्जेक्ट का उपयोग आपके ऑब्जेक्ट को सूचित करने के लिए करता है कि स्थान को नए मान के साथ अपडेट किया गया था - मूल्य के साथ कुछ करने के लिए इस विधि को लागू करने के लिए आप पर निर्भर है। – quellish