gpt4 book ai didi

arrays - 如何保存 [GMSPolyline] 以便在用户关闭和打开应用程序时它仍然保留数据

转载 作者:行者123 更新时间:2023-11-28 08:22:40 25 4
gpt4 key购买 nike

你好,在我当前的项目中我有:

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!
}

}

以及另一个类:

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)”。它说“无法识别的选择器发送到实例”。谁能帮我?有更简单的方法吗?谢谢!

最佳答案

您正在复制的代码有点旧,并且 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")
}
}

关于arrays - 如何保存 [GMSPolyline] 以便在用户关闭和打开应用程序时它仍然保留数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40880141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com