我已经问了很长时间的问题,我真的不明白为什么会这样。我已经在网上搜索了解决方案,我已经尝试了每一个,但我仍然遇到了这个问题。我在Swift中初始化一个字典,然后我添加了一些值,但是在我添加了键/值之后,字典为空(在调试模式下检查),在尝试读取时代码中也是零从中。这是我目前的代码:
我首先得到我要添加词典的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仍为空。如果我使用&#39;路径&#39;而不是&#39; bundlepath&#39;它有效!
dict = NSMutableDictionary(contentsOfFile: bundlepath)! //<- I USED 'path' INSTEAD OF 'bundlepath' and now it works!
questionsArr = dict.objectForKey("Math") as NSArray
答案 0 :(得分:2)
您正在尝试编写iOS Bundle。 iOS Bundle是只读的,你不能写。
我建议你写一下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)
//将数组分配回字典。 DICT [ “数学”] = questionsMutableArray
dict.writeToFile(path!, atomically: true)
var dictFromDisk = NSDictionary(contentsOfFile: path)!
答案 1 :(得分:2)
首先检索plist时,将其检索到NSMutableDictionary
var dict = NSMutableDictionary(contentsOfFile: path!)!
然后,一旦创建了数组,就将字典Math键设置为它。
dict["math"] = questionsMutableArray
然后将字典写回您的路径。可能想看看它是否有效,writeToFile返回一个bool。
let didItSave = dict.writeToFile(path!, atomically: true)
答案 2 :(得分:0)
您的plist文件是一个字典数组,因此当您检索它时,您必须将其作为NSArray检索,然后将其转换为Dictionary<String, String>
的数组
let array = NSArray(contentsOfFile: NSBundle.mainBundle().pathForResource("Questions", ofType: "plist")!) as [[String: String]]
然后使用以下方法访问此数组中的特定字典:
let someDict = array[someIndex]
答案 3 :(得分:0)
编辑我的回答:
您正在使用以下行保存数组
questionsMutableArray.writeToFile(path!, atomically: true)
然后尝试将其加载为字典。你应该将问题数组设置为&#34;数学&#34;字典的键,然后像这样保存字典本身,使您的读写兼容。
dict["Math"] = questionsMutableArray
dict.writeToFile(path!, atomically: true)