一旦appclosures,如何保存Pin注解(Swift)
目前,我的应用钉住一个位置,该位置是这样的商店:
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的指导方针/结构将不胜感激!
你可以保存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) } }