Swift 词典已满但仍为空
Swift Dictionary filled but still empty
我在一个问题上坐了太久了,我真的不明白为什么会这样。我在网上搜索了解决方案,并且尝试了每一个解决方案,但仍然遇到这个问题。我在 Swift 中初始化了一个字典,然后向其中添加了一些值,但是在我添加 keys/values 之后,字典立即为空(在调试模式下检查),稍后在代码中也为 nil当试图从中读取时。这是我当前的代码:
我首先获取要添加字典的 plist:
var path = NSBundle.mainBundle().pathForResource("Questions", ofType: "plist")
var dict = NSDictionary(contentsOfFile: path!)!
var questionsArr = dict.objectForKey("Math") as NSArray
var questionsMutableArray:NSMutableArray = questionsArr.mutableCopy() as NSMutableArray
然后我创建值并将其添加到我的字典
//I have also tried with this and got the same result: var newDict = Dictionary<String, String>()
var newDict = [String: String]()
newDict["Question"] = "What is 1+1"
newDict["A"] = "One"
newDict["B"] = "Two"
newDict["C"] = "Three"
newDict 在调试模式下似乎是空的。
然后我把字典写到我的 plist:
questionsMutableArray.addObject(newDict)
questionsMutableArray.writeToFile(path!, atomically: true)
稍后在代码中,我得到了这样的 plist(在我没有添加 newDict 之前它起作用了:
var path = NSBundle.mainBundle().pathForResource("Questions", ofType: "plist")
var dict = NSDictionary(contentsOfFile: path!)!
在最后一行 (NSDictinoary(contentsOfFile:path!)! 我得到这个错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
有谁知道为什么会这样?
帮助将不胜感激!
编辑 4)
中的解决方案
1. I'm getting an array of dictionaries from my plist (Questions).
2. I add a new dictionary to my array of dictionaries.
3. I write/save it to the plist.
4. I get the array from the plist again and investigate the content of it.
5. The new dictionary I just added is not there.
这是代码:
1) questionsArr 为空
var bundlepath:NSString = NSBundle.mainBundle().pathForResource("Questions", ofType: "plist")!
var dict = NSMutableDictionary(contentsOfFile: bundlepath)!
var questionsArr = dict.objectForKey("Math") as NSArray
var questionsMutableArray:NSMutableArray = questionsArr.mutableCopy() as NSMutableArray
2) questionsMutableArray 是空的,现在里面有新问题。
var newDict = [String: String]()
newDict["QuestionTitle"] = "What is 1+1?"
newDict["A"] = "One"
newDict["B"] = "Two"
newDict["C"] = "Three"
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
var path:NSString = documentsPath.stringByAppendingPathComponent("Questions.plist");
questionsMutableArray.addObject(newDict)
dict["Math"] = questionsMutableArray
3)
let didItSave = dict.writeToFile(path, atomically: true)
4) questionsArr 还是空的。如果我使用 'path' 而不是 'bundlepath' 就可以了!
dict = NSMutableDictionary(contentsOfFile: bundlepath)! //<- I USED 'path' INSTEAD OF 'bundlepath' and now it works!
questionsArr = dict.objectForKey("Math") as NSArray
你的 plist 文件是一个字典数组,所以当你检索它时,你必须将它检索为一个 NSArray,然后将它转换为一个 Dictionary<String, String>
的数组
let array = NSArray(contentsOfFile: NSBundle.mainBundle().pathForResource("Questions", ofType: "plist")!) as [[String: String]]
然后您使用以下方法访问此数组中的特定词典:
let someDict = array[someIndex]
编辑了我的回答:
您正在使用以下行保存数组
questionsMutableArray.writeToFile(path!, atomically: true)
但随后尝试将其作为字典加载。您应该将问题数组设置为字典的 "Math" 键,然后像这样保存字典本身以使您的读写兼容。
dict["Math"] = questionsMutableArray
dict.writeToFile(path!, atomically: true)
您正在尝试编写 iOS 捆绑包。 iOS 包是只读的,你不能写。
我建议你写在Document目录
试试这个
var bundlepath:NSString= NSBundle.mainBundle().pathForResource("Questions", ofType: "plist")
var dict = NSDictionary(contentsOfFile: bundlepath)!
var questionsArr = dict.objectForKey("Math") as NSArray
var questionsMutableArray:NSMutableArray = questionsArr.mutableCopy() as NSMutableArray
//I have also tried with this and got the same result: var newDict = Dictionary<String, String>()
var newDict = [String: String]()
newDict["Question"] = "What is 1+1"
newDict["A"] = "One"
newDict["B"] = "Two"
newDict["C"] = "Three"
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
var path:NSString = documentsPath.stringByAppendingPathComponent("Questions.plist");
questionsMutableArray.addObject(newDict)
//将数组赋值回字典。
字典["Math"]=questionsMutableArray
dict.writeToFile(path!, atomically: true)
var dictFromDisk = NSDictionary(contentsOfFile: path)!
首先当你检索 plist 时,将它检索到一个 NSMutableDictionary
var dict = NSMutableDictionary(contentsOfFile: path!)!
然后,一旦你创建了你的数组,将你的字典 Math 键设置为它。
dict["math"] = questionsMutableArray
然后把字典写回你的路径。可能想看看它是否有效,writeToFile returns a bool.
let didItSave = dict.writeToFile(path!, atomically: true)
我在一个问题上坐了太久了,我真的不明白为什么会这样。我在网上搜索了解决方案,并且尝试了每一个解决方案,但仍然遇到这个问题。我在 Swift 中初始化了一个字典,然后向其中添加了一些值,但是在我添加 keys/values 之后,字典立即为空(在调试模式下检查),稍后在代码中也为 nil当试图从中读取时。这是我当前的代码:
我首先获取要添加字典的 plist:
var path = NSBundle.mainBundle().pathForResource("Questions", ofType: "plist")
var dict = NSDictionary(contentsOfFile: path!)!
var questionsArr = dict.objectForKey("Math") as NSArray
var questionsMutableArray:NSMutableArray = questionsArr.mutableCopy() as NSMutableArray
然后我创建值并将其添加到我的字典
//I have also tried with this and got the same result: var newDict = Dictionary<String, String>()
var newDict = [String: String]()
newDict["Question"] = "What is 1+1"
newDict["A"] = "One"
newDict["B"] = "Two"
newDict["C"] = "Three"
newDict 在调试模式下似乎是空的。 然后我把字典写到我的 plist:
questionsMutableArray.addObject(newDict)
questionsMutableArray.writeToFile(path!, atomically: true)
稍后在代码中,我得到了这样的 plist(在我没有添加 newDict 之前它起作用了:
var path = NSBundle.mainBundle().pathForResource("Questions", ofType: "plist")
var dict = NSDictionary(contentsOfFile: path!)!
在最后一行 (NSDictinoary(contentsOfFile:path!)! 我得到这个错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
有谁知道为什么会这样? 帮助将不胜感激!
编辑 4)
中的解决方案1. I'm getting an array of dictionaries from my plist (Questions).
2. I add a new dictionary to my array of dictionaries.
3. I write/save it to the plist.
4. I get the array from the plist again and investigate the content of it.
5. The new dictionary I just added is not there.
这是代码:
1) questionsArr 为空
var bundlepath:NSString = NSBundle.mainBundle().pathForResource("Questions", ofType: "plist")!
var dict = NSMutableDictionary(contentsOfFile: bundlepath)!
var questionsArr = dict.objectForKey("Math") as NSArray
var questionsMutableArray:NSMutableArray = questionsArr.mutableCopy() as NSMutableArray
2) questionsMutableArray 是空的,现在里面有新问题。
var newDict = [String: String]()
newDict["QuestionTitle"] = "What is 1+1?"
newDict["A"] = "One"
newDict["B"] = "Two"
newDict["C"] = "Three"
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
var path:NSString = documentsPath.stringByAppendingPathComponent("Questions.plist");
questionsMutableArray.addObject(newDict)
dict["Math"] = questionsMutableArray
3)
let didItSave = dict.writeToFile(path, atomically: true)
4) questionsArr 还是空的。如果我使用 'path' 而不是 'bundlepath' 就可以了!
dict = NSMutableDictionary(contentsOfFile: bundlepath)! //<- I USED 'path' INSTEAD OF 'bundlepath' and now it works!
questionsArr = dict.objectForKey("Math") as NSArray
你的 plist 文件是一个字典数组,所以当你检索它时,你必须将它检索为一个 NSArray,然后将它转换为一个 Dictionary<String, String>
let array = NSArray(contentsOfFile: NSBundle.mainBundle().pathForResource("Questions", ofType: "plist")!) as [[String: String]]
然后您使用以下方法访问此数组中的特定词典:
let someDict = array[someIndex]
编辑了我的回答:
您正在使用以下行保存数组
questionsMutableArray.writeToFile(path!, atomically: true)
但随后尝试将其作为字典加载。您应该将问题数组设置为字典的 "Math" 键,然后像这样保存字典本身以使您的读写兼容。
dict["Math"] = questionsMutableArray
dict.writeToFile(path!, atomically: true)
您正在尝试编写 iOS 捆绑包。 iOS 包是只读的,你不能写。
我建议你写在Document目录 试试这个
var bundlepath:NSString= NSBundle.mainBundle().pathForResource("Questions", ofType: "plist")
var dict = NSDictionary(contentsOfFile: bundlepath)!
var questionsArr = dict.objectForKey("Math") as NSArray
var questionsMutableArray:NSMutableArray = questionsArr.mutableCopy() as NSMutableArray
//I have also tried with this and got the same result: var newDict = Dictionary<String, String>()
var newDict = [String: String]()
newDict["Question"] = "What is 1+1"
newDict["A"] = "One"
newDict["B"] = "Two"
newDict["C"] = "Three"
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
var path:NSString = documentsPath.stringByAppendingPathComponent("Questions.plist");
questionsMutableArray.addObject(newDict)
//将数组赋值回字典。 字典["Math"]=questionsMutableArray
dict.writeToFile(path!, atomically: true)
var dictFromDisk = NSDictionary(contentsOfFile: path)!
首先当你检索 plist 时,将它检索到一个 NSMutableDictionary
var dict = NSMutableDictionary(contentsOfFile: path!)!
然后,一旦你创建了你的数组,将你的字典 Math 键设置为它。
dict["math"] = questionsMutableArray
然后把字典写回你的路径。可能想看看它是否有效,writeToFile returns a bool.
let didItSave = dict.writeToFile(path!, atomically: true)