如何保存 [GMSPolyline] 以便在用户关闭和打开应用程序时它仍然保留数据
How to save a [GMSPolyline] so that when the user closes and opens the app it still retains the data
你好,在我目前的项目中我有:
class SecondController: UIViewController, CLLocationManagerDelegate {
var allPoly : [GMSPolyline] = []
private let rootKey = "rootKey"
func applicationWillResignActive(notification: NSNotification)
{
let filePath = self.dataFilePath()
let savedPolys = SavedPolys()
let array = self.allPoly as NSArray
savedPolys.alPoly = array as? [GMSPolyline]
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWith: data)
archiver.encode(savedPolys, forKey: rootKey)
archiver.finishEncoding()
data.write(toFile: filePath, atomically: true)
}
func dataFilePath() -> String
{
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentsDirectory = paths[0] as NSString
return documentsDirectory.appendingPathComponent("data.archive") as String
}
func buttonPressed()
{
//adds a polyline to allPoly!
}
}
以及另一个 class:
import Foundation
import GoogleMaps
class SavedPolys: NSObject, NSCoding, NSCopying
{
var alPoly: [GMSPolyline]?
let polyKey = "polyKey"
override init()
{
}
required init?(coder aDecoder: NSCoder)
{
alPoly = aDecoder.decodeObject(forKey: polyKey) as? [GMSPolyline]
}
func encode(with aCoder: NSCoder) {
if let savePoly = alPoly
{
aCoder.encode(savePoly, forKey: polyKey)
}
}
func copy(with zone: NSZone? = nil) -> Any {
let copy = SavedPolys()
if let polysToCopy = alPoly
{
var newPolys = Array<GMSPolyline>()
for poly in polysToCopy
{
newPolys.append(poly)
}
copy.alPoly = newPolys
}
return copy
}
}
我试图做到这一点,如果用户将多边形添加到 allPoly 数组,然后关闭他们的应用程序,该数组将被保存,然后在他们打开应用程序时重新加载。我已经尝试按照我的 Class' 教科书中的一章(这是所有这些内容的来源)进行操作,但是当前代码在这一行给我一个错误:"archiver.encode(savedPolys, forKey: rootKey)"。它说 "unrecognized selector sent to instance"。谁能帮我?有更简单的方法吗?谢谢!
您正在复制的代码有点旧,并且 API 已经更改,这就是它抛出错误的原因。下面是修改后的代码:
Swift 2.3
class SavedPolys : NSObject, NSCoding, NSCopying
{
var alPoly: [GMSPolyline]?
let polyKey = "polyKey"
override init()
{
}
required init?(coder aDecoder: NSCoder)
{
alPoly = aDecoder.decodeObjectForKey(polyKey) as? [GMSPolyline]
}
func encodeWithCoder(aCoder: NSCoder)
{
if let savePoly = alPoly
{
aCoder.encodeObject(savePoly, forKey: polyKey)
}
}
func copyWithZone(zone: NSZone) -> AnyObject
{
let copy = SavedPolys()
if let polysToCopy = alPoly
{
var newPolys = Array<GMSPolyline>()
for poly in polysToCopy
{
newPolys.append(poly)
}
copy.alPoly = newPolys
}
return copy
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var allPoly : [GMSPolyline] = []
private let rootKey = "rootKey"
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let filePath = self.dataFilePath()
let savedPolys = SavedPolys()
let array = self.allPoly as NSArray
savedPolys.alPoly = array as? [GMSPolyline]
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
archiver.encodeObject(savedPolys, forKey: rootKey)
archiver.finishEncoding()
data.writeToURL(NSURL(fileURLWithPath: filePath), atomically: true)
return true
}
func dataFilePath() -> String
{
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as NSString
return documentsDirectory.stringByAppendingPathComponent("data.archive")
}
}
Swift 3.0
class SavedPolys : NSObject, NSCoding, NSCopying
{
var alPoly: [GMSPolyline]?
let polyKey = "polyKey"
override init()
{
}
required init?(coder aDecoder: NSCoder)
{
alPoly = aDecoder.decodeObject(forKey: polyKey) as? [GMSPolyline]
}
func encode(with aCoder: NSCoder)
{
if let savePoly = alPoly
{
aCoder.encode(savePoly, forKey: polyKey)
}
}
func copy(with zone: NSZone? = nil) -> Any
{
let copy = SavedPolys()
if let polysToCopy = alPoly
{
var newPolys = Array<GMSPolyline>()
for poly in polysToCopy
{
newPolys.append(poly)
}
copy.alPoly = newPolys
}
return copy
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var allPoly : [GMSPolyline] = []
private let rootKey = "rootKey"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let filePath = self.dataFilePath()
let savedPolys = SavedPolys()
let array = self.allPoly as NSArray
savedPolys.alPoly = array as? [GMSPolyline]
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWith: data)
archiver.encode(savedPolys, forKey: rootKey)
archiver.finishEncoding()
data.write(to: NSURL(fileURLWithPath: filePath) as URL, atomically: true)
return true
}
func dataFilePath() -> String
{
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0] as NSString
return documentsDirectory.appendingPathComponent("data.archive")
}
}
你好,在我目前的项目中我有:
class SecondController: UIViewController, CLLocationManagerDelegate {
var allPoly : [GMSPolyline] = []
private let rootKey = "rootKey"
func applicationWillResignActive(notification: NSNotification)
{
let filePath = self.dataFilePath()
let savedPolys = SavedPolys()
let array = self.allPoly as NSArray
savedPolys.alPoly = array as? [GMSPolyline]
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWith: data)
archiver.encode(savedPolys, forKey: rootKey)
archiver.finishEncoding()
data.write(toFile: filePath, atomically: true)
}
func dataFilePath() -> String
{
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentsDirectory = paths[0] as NSString
return documentsDirectory.appendingPathComponent("data.archive") as String
}
func buttonPressed()
{
//adds a polyline to allPoly!
}
}
以及另一个 class:
import Foundation
import GoogleMaps
class SavedPolys: NSObject, NSCoding, NSCopying
{
var alPoly: [GMSPolyline]?
let polyKey = "polyKey"
override init()
{
}
required init?(coder aDecoder: NSCoder)
{
alPoly = aDecoder.decodeObject(forKey: polyKey) as? [GMSPolyline]
}
func encode(with aCoder: NSCoder) {
if let savePoly = alPoly
{
aCoder.encode(savePoly, forKey: polyKey)
}
}
func copy(with zone: NSZone? = nil) -> Any {
let copy = SavedPolys()
if let polysToCopy = alPoly
{
var newPolys = Array<GMSPolyline>()
for poly in polysToCopy
{
newPolys.append(poly)
}
copy.alPoly = newPolys
}
return copy
}
}
我试图做到这一点,如果用户将多边形添加到 allPoly 数组,然后关闭他们的应用程序,该数组将被保存,然后在他们打开应用程序时重新加载。我已经尝试按照我的 Class' 教科书中的一章(这是所有这些内容的来源)进行操作,但是当前代码在这一行给我一个错误:"archiver.encode(savedPolys, forKey: rootKey)"。它说 "unrecognized selector sent to instance"。谁能帮我?有更简单的方法吗?谢谢!
您正在复制的代码有点旧,并且 API 已经更改,这就是它抛出错误的原因。下面是修改后的代码:
Swift 2.3
class SavedPolys : NSObject, NSCoding, NSCopying
{
var alPoly: [GMSPolyline]?
let polyKey = "polyKey"
override init()
{
}
required init?(coder aDecoder: NSCoder)
{
alPoly = aDecoder.decodeObjectForKey(polyKey) as? [GMSPolyline]
}
func encodeWithCoder(aCoder: NSCoder)
{
if let savePoly = alPoly
{
aCoder.encodeObject(savePoly, forKey: polyKey)
}
}
func copyWithZone(zone: NSZone) -> AnyObject
{
let copy = SavedPolys()
if let polysToCopy = alPoly
{
var newPolys = Array<GMSPolyline>()
for poly in polysToCopy
{
newPolys.append(poly)
}
copy.alPoly = newPolys
}
return copy
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var allPoly : [GMSPolyline] = []
private let rootKey = "rootKey"
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let filePath = self.dataFilePath()
let savedPolys = SavedPolys()
let array = self.allPoly as NSArray
savedPolys.alPoly = array as? [GMSPolyline]
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
archiver.encodeObject(savedPolys, forKey: rootKey)
archiver.finishEncoding()
data.writeToURL(NSURL(fileURLWithPath: filePath), atomically: true)
return true
}
func dataFilePath() -> String
{
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as NSString
return documentsDirectory.stringByAppendingPathComponent("data.archive")
}
}
Swift 3.0
class SavedPolys : NSObject, NSCoding, NSCopying
{
var alPoly: [GMSPolyline]?
let polyKey = "polyKey"
override init()
{
}
required init?(coder aDecoder: NSCoder)
{
alPoly = aDecoder.decodeObject(forKey: polyKey) as? [GMSPolyline]
}
func encode(with aCoder: NSCoder)
{
if let savePoly = alPoly
{
aCoder.encode(savePoly, forKey: polyKey)
}
}
func copy(with zone: NSZone? = nil) -> Any
{
let copy = SavedPolys()
if let polysToCopy = alPoly
{
var newPolys = Array<GMSPolyline>()
for poly in polysToCopy
{
newPolys.append(poly)
}
copy.alPoly = newPolys
}
return copy
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var allPoly : [GMSPolyline] = []
private let rootKey = "rootKey"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let filePath = self.dataFilePath()
let savedPolys = SavedPolys()
let array = self.allPoly as NSArray
savedPolys.alPoly = array as? [GMSPolyline]
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWith: data)
archiver.encode(savedPolys, forKey: rootKey)
archiver.finishEncoding()
data.write(to: NSURL(fileURLWithPath: filePath) as URL, atomically: true)
return true
}
func dataFilePath() -> String
{
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0] as NSString
return documentsDirectory.appendingPathComponent("data.archive")
}
}