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

初めて true を返します。次回呼び出すたびに、メソッドは false を返します。

私はいくつかのSOを読みました.いくつかの投稿では、この方法でデータを書き換えることはできません. しかし、私は試しました:

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

それでも役に立ちませんでした。

私がしたこと:

  1. すべてのモデル ファイルでNSCodingプロトコルを確認しました
  2. required init(coder aDecoder: NSCoder)私とすべてをチェックしたfunc encodeWithCoder(aCoder: NSCoder)

最後のアプリでこれを行ったので、何かが欠けています。

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 を使用しています。

ヘルプとコード修正は大歓迎です。

4

2 に答える 2