应用关闭后如何保存Pin Annotation(Swift) [英] How to save Pin Annotation once app is closed (Swift)

查看:92
本文介绍了应用关闭后如何保存Pin Annotation(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我的应用固定了一个位置,并且该位置的存储方式如下:

Currently, my app pins a location and that location is stores like so:

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);
}

where locationDefaults是存储引脚位置的数组.

where locationsDefaults is the array where I store the location of the pin.

我想使用NSUserdefaults,但是我不太确定该怎么做,因为除了非NS数据外,它不会.快速的指导方针/结构将不胜感激!

I would like to use NSUserdefaults, but i'm not really sure how i'd do that as it doesn't except non NS data. A guideline/structure in swift would be greatly appreciated!

推荐答案

您可以将NSData保存到NSUserDefaults.因此,您所需要做的就是使用 NSKeyedArchiver 方法

You can save NSData to NSUserDefaults. So all you need is to convert your CLLocation object to NSData using NSKeyedArchiver method archivedDataWithRootObject as follow:

// 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)
    }
}

这篇关于应用关闭后如何保存Pin Annotation(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆