NSKeyedArchiver.archiveRootObject返回" false"如果我想覆盖数据,为什么?

时间:2015-11-08 16:45:16

标签: swift xcode6 nskeyedarchiver ios9.1

NSKeyedArchiver.archiveRootObject(<#rootObject: AnyObject#>, toFile: <#String#>)

仅在第一次返回true。每次下次调用它时,该方法都返回false。

我读了一些SO,有些帖子说我不能用这种方式重写数据。但是,我试过了:

 NSFileManager.defaultManager().removeItemAtPath(path, error: nil) 

它仍然没有帮助。

我做了什么:

  1. 检查了NSCoding协议
  2. 的所有模型文件
  3. 检查了我的所有required init(coder aDecoder: NSCoder)func encodeWithCoder(aCoder: NSCoder)
  4. 我遗漏了一些东西,因为我在我的上一个应用程序中完成了这个并且它工作了fla`

    import Foundation
    
        private let ON_DISK_DATA_DICTIONARY    = "savedDataPathsOnDisk"
        private let _WBMAccessDataOnDiskMShared = WBMAccessDataOnDiskM()
        private var dataDirectories:NSArray!   = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        private var dataDirectoryURL:NSURL!    = NSURL(fileURLWithPath: dataDirectories.objectAtIndex(0) as! String, isDirectory: true)
        private var dataDirectoryPath:String!  = dataDirectoryURL.path!
    
        let FILE_FORMAT = ".archive"
    
        class WBMAccessDataOnDiskM: NSObject
        {
            class var sharedData: WBMAccessDataOnDiskM
            {
                return _WBMAccessDataOnDiskMShared
            }
    
            private var dataAndPathDictionary = [String:String]()
    
            func getDataAndPathDictionary() -> [String:String]
            {
                return self.dataAndPathDictionary
            }
    
            func addDataAndPathToDictionary(data:String ,path:String)
            {
                if !checkIfDataAllreadyExists(data)
                {
                    let fullPath                = createFullDataPath(path)
                    dataAndPathDictionary[data] = fullPath
                    NSUserDefaults.standardUserDefaults().setObject(dataAndPathDictionary, forKey: ON_DISK_DATA_DICTIONARY)
                }
            }
    
            func checkIfDataIsAvailable(dataPathComponent:String) -> (Bool,String)
            {
                var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
                var dataPath = paths.stringByAppendingPathComponent(dataPathComponent)
                var checkValidation = NSFileManager.defaultManager()
    
                println(dataPathComponent)
    
                if (checkValidation.fileExistsAtPath(dataPath))
                {
                    return (true,dataPath)
                }
                else
                {
                    return (false,"")
                }
            }
    
            func checkForDataOnDisk() -> Bool
            {
                let dataDict = NSUserDefaults.standardUserDefaults().objectForKey(ON_DISK_DATA_DICTIONARY) as? [String:String]
    
                if dataDict == nil
                {
                    return false
                }
                else
                {
                    dataAndPathDictionary = dataDict!
                    return true
                }
            }
    
            private func checkIfDataAllreadyExists(data:String) -> Bool
            {
                let keys = self.dataAndPathDictionary.keys.array
                if contains(keys, data)
                {
                    return true
                }
                return false
            }
    
            private func createFullDataPath(path:String) -> String
            {
                var fullPathURL = dataDirectoryURL.URLByAppendingPathComponent(path + FILE_FORMAT)
                return fullPathURL.path!
            }
    
            func saveDataArray(data:[AnyObject], path:String)
            {
                NSFileManager.defaultManager().removeItemAtPath(path, error: nil)
    
                if NSKeyedArchiver.archiveRootObject(data, toFile: path)
                {
                   // SAVING
                    println(" Saving data ARRAY ")
                }
                else
                {
                    println(" NOT saving data ARRAY ")
                }
            }
    
            func saveDataObject(dataObject:AnyObject, path:String)
            {
                if NSKeyedArchiver.archiveRootObject(dataObject, toFile: path)
                {
                    println(" Saving data OBJECT ")
                }
                else
                {
                    println(" NOT saving data OBJECT ")
                }
            }
    
            // dataFromDisk = NSKeyedUnarchiver.unarchiveObjectWithFile(pathForNews) as? [AnyObject]
            func loadDataArray(path:String) -> [AnyObject]?
            {
                var dataArrayFromDisk: [AnyObject]?
                dataArrayFromDisk = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? [AnyObject]
    
                return dataArrayFromDisk
            }
    
            func loadDataObject(path:String) -> AnyObject?
            {
                var dataObjectFromDisk: AnyObject?
                dataObjectFromDisk = NSKeyedUnarchiver.unarchiveObjectWithFile(path)
                return dataObjectFromDisk
            }
    
            func getNewsDataLanguagePath() -> String
            {
                var currentOSLanguage = LOCALIZATION.currentOsLanguage
                currentOSLanguage     = currentOSLanguage.substringToIndex(2)
                if currentOSLanguage == "de"
                {
                    return ON_DISK_CONTENT_DE
                }
                else if currentOSLanguage == "en"
                {
                    return ON_DISK_CONTENT_ENG
                }
                return ON_DISK_CONTENT_ENG
            }
        `
    

    我正在使用Xcode 6.4和Swift 1.2。

    任何帮助&amp;我们欢迎代码更正。

2 个答案:

答案 0 :(得分:1)

由于您放在此处的代码不包含saveDataArraysaveDataObject的调用,因此我判断您已手动维护已归档对象的路径。这是哪里出错了。名为archiveRootObject的NSKeyedArchiver方法可以自动维护归档文件路径。

在Apple的doucumet中

  

通过将对象图形编码为数据对象来存档以给定对象为根的对象图,然后将结果数据对象原子地写入给定路径的文件,并返回一个布尔值,指示操作是否成功。

SO还有另一个问题可以帮到你。

答案 1 :(得分:0)

我在这个好例子中遵循了苹果指令:Persist Data 但是我用AppleTV的应用程序描述了同样的问题。最后,我更改了CacheDirectory的.Documents目录,并且运行良好。

static let DocumentsDirectorio = NSFileManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first!