안녕을 유지 응용 프로그램을 열 때 내가 가지고 :저장하는 방법 [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
}
}
을
사용자가 allPoly 배열에 polys를 추가 한 다음 해당 응용 프로그램을 닫으면 배열이 저장되고 응용 프로그램을 열 때 다시로드됩니다. 나는 클래스 '교과서 (이 모든 것이 어디에서 오는 것인가)에서 장을 따라 가려고했지만이 코드는 나에게이 줄에 오류를 준다 : "archiver.encode (savedPolys, forKey : rootKey)". "인스턴스로 보낸 인식 할 수없는 선택기"라고 표시됩니다. 누구든지 나를 도울 수 있습니까? 더 간단한 방법이 있습니까? 감사!
당신이 UserDefaults.standard 물건을 사용하여 의미합니까 다음은 수정 된 코드인가? – skyleguy
왜 내 질문에 대한 의견을 묻고 있습니까? :) –
내가 응답했지만 D가지나 갔다는 코멘트가있었습니다. LOL이게 지금 우스운 것 같아요. – skyleguy