目前,我的应用程序固定了一个位置,该位置就是这样的商店:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
// Find location of user
var userLocation:CLLocation = locations[0] as! CLLocation
var latitude = userLocation.coordinate.latitude
var longitude = userLocation.coordinate.longitude
var latDelta:CLLocationDegrees = 0.01
var longDelta: CLLocationDegrees = 0.01
var span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var location:MKUserLocation = currentLocation;
var region: MKCoordinateRegion = MKCoordinateRegionMake(location.coordinate, span)
var coordinate:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude);
carInitialLocation = userLocation;
locationsDefaults.append(carInitialLocation);
carInitialCoordinate = coordinate;
self.map.setRegion(region, animated: true);
}
其中locationsDefaults是我存储引脚位置的数组。
我想使用NSUserdefaults,但我不确定我是怎么做的,因为除了非NS数据之外没有。 swift的指南/结构将非常感谢!
答案 0 :(得分:0)
您可以将NSData保存到NSUserDefaults。所以你需要的是使用NSKeyedArchiver方法archivedDataWithRootObject将你的CLLocation对象转换为NSData,如下所示:
// saving your CLLocation object
let locationData = NSKeyedArchiver.archivedDataWithRootObject(carInitialLocation)
NSUserDefaults.standardUserDefaults().setObject(locationData, forKey: "locationData")
// loading it
if let loadedData = NSUserDefaults.standardUserDefaults().dataForKey("locationData") {
if let loadedLocation = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData) as? CLLocation {
println(loadedLocation.coordinate.latitude)
println(loadedLocation.coordinate.longitude)
}
}