2017-03-20 54 views
0

我無法檢索到在Swift 3中保存爲NSkeyed存檔的對象,並且正在縮小我的頭。對象保存成功是plist,但裝回時返回零無法在Swift3中檢索正確保存的NSKeyArchived對象

這裏是我使用的代碼:

類本身保存爲一個對象是相當容易:

import Foundation 

class ItemList:NSObject, NSCoding { 

    var name: String = "" //Name of the Item list 
    var contents: [Int] = [] //Ints referencing the CoreData PackItems 

    init (listname:String, ContentItems:[Int]) { 
     self.name=listname 
     self.contents=ContentItems 
    } 

    //MARK: NSCoding 
    public convenience required init?(coder aDecoder: NSCoder) { 
     let thename = aDecoder.decodeObject(forKey: "name") as! String 
     let thecontents = aDecoder.decodeObject(forKey: "contents") as! [Int] 
     self.init(listname: thename,ContentItems: thecontents) 
    } 

    func encode(with aCoder: NSCoder) { 
     aCoder.encode(self.name,forKey:"name") 
     aCoder.encode(self.contents, forKey: "contents") 
    } 

} 

加載和保存對象的代碼:

class FileHandler: NSObject { 

    class func getDocumentsDirectory() -> URL { 
     let filemgr = FileManager.default 
     let urls = filemgr.urls(for: .documentDirectory, in: .userDomainMask) 
     let result:URL = urls.first! 
     return result 
    } 

    ///This returns the contents of the handed file inside the Documents directory as the object it was saved as. 
    class func getFileAsObject(filename:String) -> AnyObject? { 

     let path = getDocumentsDirectory().appendingPathComponent(filename) 

     if let result = NSKeyedUnarchiver.unarchiveObject(withFile: path.absoluteString) { 
      //Success 
      print("Loaded file '"+filename+"' from storage") 
      return result as AnyObject? 
     } else { 
      print("Error: Couldn't find requested object '"+filename+"' in storage at "+path.absoluteString) 
      return nil 
     } 
    } 

    ///This saves the handed object under the given filename in the App's Documents directory. 
    class func saveObjectAsFile(filename:String, Object:AnyObject) { 
     let data = NSKeyedArchiver.archivedData(withRootObject: Object) 
     let fullPath = getDocumentsDirectory().appendingPathComponent(filename) 

     do { 
      try data.write(to: fullPath) 
      print("Wrote file '"+filename+"' to storage at "+fullPath.absoluteString) 
     } catch { 
      print("Error: Couldn't write file '"+filename+"' to storage") 
     } 
    } 

} 

...最後,這是我做的叫了這一切:

let testobject:ItemList = ItemList.init(listname: "testlist", ContentItems: [0,0,1,2]) 

     FileHandler.saveObjectAsFile(filename:"Test.plist",Object:testobject) 
     let tobi = FileHandler.getFileAsObject(filename:"Test.plist") as! ItemList 

唉,我得到這個作爲輸出:

Wrote file 'Test.plist' to storage at file:///…/data/Containers/Data/Application/6747B038-B0F7-4B77-85A8-9EA02BC574FE/Documents/Test.plist 
Error: Couldn't find requested object 'Test.plist' in storage at file:///…/data/Containers/Data/Application/6747B038-B0F7-4B77-85A8-9EA02BC574FE/Documents/Test.plist 

請注意,這是我自己的輸出 - 所以我做的(並已選中),該文件被正確創建。但它不會加載。誰能告訴我我做錯了什麼?

回答

2

問題出在您傳遞給unarchiveObject(withFile:)的路徑上。

變化:

if let result = NSKeyedUnarchiver.unarchiveObject(withFile: path.absoluteString) { 

到:

if let result = NSKeyedUnarchiver.unarchiveObject(withFile: path.path) { 

在一個側面說明,你應該使用對稱的API爲你的寫作和閱讀的邏輯。在寫入數據時,將根對象存檔到Data對象,然後將Data對象寫入文件。但是在閱讀時,您可以直接取消存檔給定文件路徑的對象樹。

請更改您的書寫代碼以使用archiveRootObject(_:toFile:)或更改讀取代碼以從文件加載Data,然後取消存檔數據。你當前的代碼有效(一旦你解決了路徑問題)但它不一致。

+0

謝謝Maddy,這是一個出色而簡潔的答案! – Averett