如何将经度和纬度值(在应用运行时均显示一个纬度)设置为全局常量?这些值是当前

String(format: "4.4f", latestLocation.coordinate.latitude)


String(format: "4.4f", latestLocation.coordinate.longitude)

我想使它们成为可以在函数外部访问的全局常量,例如userlatitudeuserlongitude。我是否必须退还有意义的希望。
func locationManager(manager: CLLocationManager!,
    didUpdateLocations locations: [AnyObject]!)
{
    var latestLocation = locations.last as! CLLocation

    latitude.text = String(format: "%.4f",
        latestLocation.coordinate.latitude)
    longitude.text = String(format: "%.4f",
        latestLocation.coordinate.longitude)
}

最佳答案

这样,您可以将Locations存储到NSUserDefaults:

//First Convert it to NSNumber.
let lat : NSNumber = NSNumber(double: Location.latitude)
let lng : NSNumber = NSNumber(double: Location.longitude)

//Store it into Dictionary
let locationDict = ["lat": lat, "lng": lng]

//Store that Dictionary into NSUserDefaults
NSUserDefaults.standardUserDefaults().setObject(locationDict, forKey: "Location")

之后,您可以在任意位置以这种方式访问​​它:
//Access that stored Values
let userLoc = NSUserDefaults.standardUserDefaults().objectForKey("Location") as! [String : NSNumber]

//Get user location from that Dictionary
let userLat = userLoc["lat"]
let userLng = userLoc["lng"]

var Annotation = MKPointAnnotation()

Annotation.coordinate.latitude = userLat as! CLLocationDegrees  //Convert NSNumber to CLLocationDegrees
Annotation.coordinate.longitude = userLng as! CLLocationDegrees //Convert NSNumber to CLLocationDegrees

关于ios - 如何在Swift中的func中为全局常量创造值(value),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30998578/

10-10 20:52