2016-11-30 1 views
1

안녕을 유지 응용 프로그램을 열 때 내가 가지고 :저장하는 방법 [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)". "인스턴스로 보낸 인식 할 수없는 선택기"라고 표시됩니다. 누구든지 나를 도울 수 있습니까? 더 간단한 방법이 있습니까? 감사!

+0

당신이 UserDefaults.standard 물건을 사용하여 의미합니까 다음은 수정 된 코드인가? – skyleguy

+0

왜 내 질문에 대한 의견을 묻고 있습니까? :) –

+1

내가 응답했지만 D가지나 갔다는 코멘트가있었습니다. LOL이게 지금 우스운 것 같아요. – skyleguy

답변

0

복사하려는 코드가 약간 오래된 것이므로 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") 
    } 
} 
+0

나는 이것이 Swift 2.3과 호환된다는 것을 알지만 Swift 3.0을 사용하고 있다고 생각한다. 여전히 작동 할 것이라고 생각 하는가? – skyleguy

+0

Swift 3.0 코드도 추가했습니다. –

+0

흠. 나는 내 메인이 이미 UIViewController의 하위 클래스라고 언급하는 것을 잊어 버렸고, 따라서 UIResponder에서 서브 클래스를 허용하지 않을 것이다 : "다중 상속"오류! 어떤 아이디어? – skyleguy

관련 문제