gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:09:48 25 4
gpt4 key购买 nike

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

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

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

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

但仍然没有帮助。

我做了什么:

  1. 检查了我的所有模型文件中的 NSCoding 协议(protocol)
  2. 检查了我所有的所需的 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。

欢迎任何帮助和代码更正。

最佳答案

由于您放在这里的代码不包含saveDataArraysaveDataObject的调用,所以我判断您已经手动维护了归档对象的路径。< strong>这就是问题所在。 NSKeyedArchiver 名为 archiveRootObject 的方法可以自动维护存档文件路径。

在苹果的文档中

Archives an object graph rooted at a given object by encoding it into a data object then atomically writes the resulting data object to a file at a given path, and returns a Boolean value that indicates whether the operation was successful.

SO 中还有一个问题可能对你有帮助。

关于swift - 如果我想覆盖数据,NSKeyedArchiver.archiveRootObject 返回 "false",为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33596372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com