gpt4 book ai didi

ios - Swift 字典已填满但仍为空

转载 作者:行者123 更新时间:2023-11-28 10:25:00 24 4
gpt4 key购买 nike

我一直在解决一个问题太久了,我真的不明白为什么会这样。我在网上搜索了解决方案,并且尝试了每一个解决方案,但仍然遇到这个问题。我在 Swift 中初始化了一个字典,然后我向它添加了一些值,但是在我添加了键/值之后,字典是空的(在 Debug模式下检查),稍后在尝试读取它时在代码中也为 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 在 Debug模式下似乎是空的。然后我将字典写入我的 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还是空的。如果我使用“路径”而不是“捆绑路径”,它就可以工作!

dict = NSMutableDictionary(contentsOfFile: bundlepath)! //<- I USED 'path' INSTEAD OF 'bundlepath' and now it works!
questionsArr = dict.objectForKey("Math") as NSArray

最佳答案

您正在尝试编写 iOS Bundle。 iOS Bundle 是只读的,你不能写。

我建议你写在文档目录试试这个方法

 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["Math"]=questionsMutableArray

dict.writeToFile(path!, atomically: true)

var dictFromDisk = NSDictionary(contentsOfFile: path)!

关于ios - Swift 字典已填满但仍为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28420694/

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