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

时间:2016-11-30 04:10:23

标签: arrays swift persistence google-maps-sdk-ios polyline

你好我当前的项目:

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数组,然后关闭他们的应用程序,数组将被保存,然后在打开应用程序时重新加载。我试过跟随我的课程章节中的一章。教科书(这是所有这些来自的地方),但是这个当前的代码在这一行上给我一个错误:&#34; archiver.encode(savedPolys,forKey:rootKey)&#34;。它说&#34;无法识别的选择器被发送到实例&#34;。谁能帮我?有更简单的方法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

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