如何保存[GMSPolyline]这样,当用户关闭并打开它仍然保留在我的当前项目的数据
问题描述:
你好的应用程序我有:如何保存[GMSPolyline]这样,当用户关闭并打开它仍然保留在我的当前项目的数据
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
}
}
我想让它如此,如果用户将polys添加到allPoly数组中,然后关闭它们的应用程序,数组将被保存,然后在它们打开应用程序时重新加载。我已经尝试过从我班的教科书(这是所有这些内容的来源)出来的一章,但是这个当前的代码给了我一个这样的错误:“archiver.encode(savedPolys,forKey:rootKey)”。它说“无法识别的选择器发送到实例”。谁能帮我?有一种更简单的方法吗?谢谢!
答
您正在复制的代码有点旧,API发生了变化,这就是为什么它会抛出错误。下面是修改后的代码:
雨燕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")
}
}
雨燕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")
}
}
您的意思是使用UserDefaults.standard东西? – skyleguy
你为什么要问一个问题作为评论你自己的帖子? :) –
有一个评论那里,我回应,但它不见了D:lol我意识到这看起来很有趣 – skyleguy