Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift游乐场的核心定位_Swift_Xcode_Location_Core Location_Swift Playground - Fatal编程技术网

Swift游乐场的核心定位

Swift游乐场的核心定位,swift,xcode,location,core-location,swift-playground,Swift,Xcode,Location,Core Location,Swift Playground,我正在尝试检索用户的当前位置,但在Swift操场中。我已经有了这段代码,但出于某种原因,它从未执行print语句。有人能帮忙吗 import UIKit import CoreLocation import PlaygroundSupport var str = "Hello, playground" let locationManager = CLLocationManager() PlaygroundPage.current.needsIndefiniteExecuti

我正在尝试检索用户的当前位置,但在Swift操场中。我已经有了这段代码,但出于某种原因,它从未执行print语句。有人能帮忙吗

import UIKit  
import CoreLocation  
import PlaygroundSupport  

var str = "Hello, playground"  
let locationManager = CLLocationManager()  
PlaygroundPage.current.needsIndefiniteExecution = true  

class GeoCoordinateDelegate: NSObject, CLLocationManagerDelegate {  

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {  
        let location = locations[0]  
        print("Most Recent Location: " + location.description)  
        print(location.coordinate.latitude)  
        print(location.coordinate.longitude)  
    }  

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {  
        print("Error while updating " + error.localizedDescription)  
    }  

}  
let geoCoordinateDelegate = GeoCoordinateDelegate()  
locationManager.delegate = geoCoordinateDelegate  
locationManager.desiredAccuracy = kCLLocationAccuracyBest  
locationManager.startUpdatingLocation()  

编写CoreLocation代码时,最好先检查
CLLocationManager.locationServicesEnabled()
,然后再尝试执行任何与位置相关的操作

在Swift游乐场(2017-03-21的1.2版)中,该方法返回
false
,因为核心位置不适用于游乐场(尚未?)。我建议在