Ios swift-保存plist字典而不覆盖键
我正在创建一个便笺应用程序,我正在尝试将数据保存到plist字典中,然而,当我保存一个新便笺时,我拥有的代码将替换旧便笺 如何在不替换旧数据的情况下在字典中添加新数据Ios swift-保存plist字典而不覆盖键,ios,swift,dictionary,Ios,Swift,Dictionary,我正在创建一个便笺应用程序,我正在尝试将数据保存到plist字典中,然而,当我保存一个新便笺时,我拥有的代码将替换旧便笺 如何在不替换旧数据的情况下在字典中添加新数据 let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray let documentsDirectory = paths.objectAtIndex(0) as!
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentsDirectory = paths.objectAtIndex(0) as! NSString
let path = documentsDirectory.stringByAppendingPathComponent("notes.plist")
var dict: NSMutableDictionary = ["XInitializerItem": "DoNotEverChangeMe"]
//saving
dict.setObject(noteResult.text, forKey: nameSave.text)
//writing
dict.writeToFile(path, atomically: false)
let resultDictionary = NSMutableDictionary(contentsOfFile: path)
println("Saved note.plist file is --> \(resultDictionary?.description)")
装货注意事项:
func loadNotes(){
let plistPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let DocumentsDirectory = plistPath[0] as! String
let path = DocumentsDirectory.stringByAppendingPathComponent("notes.plist")
let fileManager = NSFileManager.defaultManager()
if (!fileManager.fileExistsAtPath(path)) {
if let bundlePath = NSBundle.mainBundle().pathForResource("notes", ofType: "plist") {
let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath)
println("Bundle notes.plist file is --> \(resultDictionary?.description)")
fileManager.copyItemAtPath(bundlePath, toPath: path, error: nil)
println("copy")
} else {
println("notes.plist not found")
}
}else {
println("note.plist already exists")
//fileManager.removeItemAtPath(path, error: nil)
}
let resultDictionary = NSMutableDictionary(contentsOfFile: path)
println("Loaded notes.plist file is --> \(resultDictionary?.description)")
var myDict = NSDictionary(contentsOfFile: path)
if let dict = myDict {
//load values
} else {
println("worning ccould not create dictionary from notes.plist, default values will be used")
}
}
第1点:从文件系统加载字典后,请您设置一个断点并再次检查,它包含以前保存的数据。这很可能是真的,那么第二点会对你有所帮助。如果此点已断开,请确保修复此点,然后将其破解:) 第2点:字典必须具有唯一键。问题在你下面的陈述中。确保每次保存新便笺时都使用新密钥保存。检查 在字典中保存数据时使用nameSave.text值
dict[nameSave.text] = noteResult.text
编辑:
我发现这里的问题是,您没有使用文件系统中已保存的文件初始化词典。您必须首先确保在文件系统中用notes.plist填充字典,然后向字典中添加新数据,最后将其保存回去
我就是这样做的;只是将您提到的两种方法组合在一个流中。在保存操作触发时调用此函数。在这里,我想,第一次检查可能不见了。我没有测试这个代码,所以请容忍我
func saveNotes() {
// First fetch old notes
let plistPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let DocumentsDirectory = plistPath[0] as! String
let path = DocumentsDirectory.stringByAppendingPathComponent("notes.plist")
let fileManager = NSFileManager.defaultManager()
if (!fileManager.fileExistsAtPath(path)) {
if let bundlePath = NSBundle.mainBundle().pathForResource("notes", ofType: "plist") {
let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath)
println("Bundle notes.plist file is --> \(resultDictionary?.description)")
fileManager.copyItemAtPath(bundlePath, toPath: path, error: nil)
println("copy")
} else {
println("notes.plist not found")
}
} else {
println("note.plist already exists")
//fileManager.removeItemAtPath(path, error: nil)
}
let resultDictionary = NSMutableDictionary(contentsOfFile: path)
println("Loaded notes.plist file is --> \(resultDictionary?.description)")
var myDict = NSDictionary(contentsOfFile: path)
if let dict = myDict {
// Save new value
dict.setObject(noteResult.text, forKey: nameSave.text)
} else {
println("worning ccould not create dictionary from notes.plist, default values will be used")
}
// Now save it back
dict.writeToFile(path, atomically: false)
}
供将来参考,解决方案是:
// First fetch old notes
let plistPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let DocumentsDirectory = plistPath[0] as! String
let path = DocumentsDirectory.stringByAppendingPathComponent("notes.plist")
let fileManager = NSFileManager.defaultManager()
if (!fileManager.fileExistsAtPath(path)) {
if let bundlePath = NSBundle.mainBundle().pathForResource("notes", ofType: "plist") {
let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath)
println("Bundle notes.plist file is --> \(resultDictionary?.description)")
fileManager.copyItemAtPath(bundlePath, toPath: path, error: nil)
println("copy")
} else {
println("notes.plist not found")
}
} else {
println("note.plist already exists")
//fileManager.removeItemAtPath(path, error: nil)
}
var myDict = NSDictionary(contentsOfFile: path)
if let dict = myDict {
// Save new value
var noteResultAll = "Date: " + saveDate.text! + "\n" + noteResult.text
dict.setValue(noteResultAll, forKey: nameSave.text)
dict.writeToFile(path, atomically: false)
} else {
println("worning ccould not create dictionary from notes.plist, default values will be used")
}
// Now save it back
let resultDictionary = NSMutableDictionary(contentsOfFile: path)
println("Loaded notes.plist file is --> \(resultDictionary?.description)")
谢谢,密钥总是以不同的值保存。nameSave.text是一个UITextField,用户可在其中选择要删除的键的值saved@SNos请看我更新的帖子。在继续添加新数据之前,请确保在字典中填充已保存的数据。谢谢@Abhinav,我知道我需要加载旧值并在保存前添加新值。但我不知道如何解决这个问题。请看我的更新帖子和我的加载功能我刚刚更新了我的答案。请检查一下@SNosNSDictionary没有成员名称“setObject”&未解析标识符“dict”的使用