我试图制作一个基本类的原型(prototype)来处理为我的应用程序获取地理坐标,并想在 Swift Playground 实例中运行它,但委托(delegate)似乎没有被调用。
下面是代码。 RE 的任何建议:调试会有所帮助。
是否可以在 IDE 中运行。
// Playground - noun: a place where people can play
import UIKit
import CoreLocation
var str = "Hello, playground"
class GeoCordDelegate: NSObject, CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
println("Updated Location")
println(locationMgr.location.coordinate.latitude)
println(locationMgr.location.coordinate.longitude)
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("Error while updating location " + error.localizedDescription)
}
}
let locationMgr = CLLocationManager()
locationMgr.delegate = GeoCordDelegate()
locationMgr.desiredAccuracy = kCLLocationAccuracyBest
locationMgr.startUpdatingLocation()
请您参考如下方法:
对于 Xcode 9
import CoreLocation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
// Address to Geocode
let address = "Champ de Mars, 5 Avenue Anatole France, 75007 Paris, France"
print("Starting Geocoding")
CLGeocoder().geocodeAddressString(address) { placemarks, error in
let coordinate = placemarks!.first!.location!.coordinate
print("Completed Geocoding, Latitude: \(coordinate.latitude), Longitude: \(coordinate.longitude)")
PlaygroundPage.current.finishExecution()
}
print("End of Playground")
