2015-02-09 61 views
0

我一直在坐在一个问题太久,我真的不明白为什么会发生这种情况。我在网上搜索了解决方案,我尝试了其中的每一个,但仍然遇到了这个问题。我在Swift中初始化一个字典,然后向它添加一些值,但是在我添加键/值后,字典是空的(在调试模式下检查),在尝试读取它时也没有在代码中出现。这是我当前的代码:Swift Dictionary filled但仍为空

我第一次得到我想要的加我的字典中的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:!路径)我得到这个错误:?!

fatal error: unexpectedly found nil while unwrapping an Optional value 

有谁知道为什么发生这种情况 帮助,将不胜感激

EDIT溶液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仍然是空的。如果我使用'路径'而不是'bundlepath',它会起作用!

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

你可以显示代码,你发现它是零。发布的代码无助于确定问题 – 2015-02-09 22:32:22

+0

不确定它是如何在swift中工作的,但是如果你想添加东西,你的字典需要是“可变的”。 – user3386109 2015-02-09 22:36:02

+1

Swift中集合的可变性取决于您是否将字典赋予变量或常量,而不是像Objective-C中的类名称。 – 2015-02-09 22:39:26

回答

2

您正在尝试编写iOS软件包。 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) 

//指定数组回字典。 字典[“加减乘除”] = questionsMutableArray

dict.writeToFile(path!, atomically: true) 

    var dictFromDisk = NSDictionary(contentsOfFile: path)! 
+0

我收到此错误:'致命错误:在执行第三行时意外发现零,同时展开可选值。 – user2099024 2015-02-09 23:13:00

+0

基本上你正在试图从bundle中加载plist,并将字典添加到数组并将其保存到磁盘。但是您必须从Document目录加载plist文件以供下次阅读,那么您的数据将保持一致。 – Shashi3456643 2015-02-09 23:28:15

+0

明天我会试试,让你知道。不能atm。提前致谢! – user2099024 2015-02-09 23:29:04

0

你的plist文件是字典的数组,所以当你找回它,你必须检索它作为一个NSArray,然后将它转换为Dictionary<String, String>

let array = NSArray(contentsOfFile: NSBundle.mainBundle().pathForResource("Questions", ofType: "plist")!) as [[String: String]] 

一个数组,那么你访问一个特定的字典这个数组中使用:

let someDict = array[someIndex] 
+0

Question.plist中的“Root”按键是一个包含数组的字典,其中也包含多个字典。 – user2099024 2015-02-09 23:01:47

+0

为什么不把你的根对象作为一个数组? – Ian 2015-02-09 23:03:08

+0

我不知道为什么我这样做,但现在它就是这样。问题是我的初始化和添加项目后,我的newDict是空的。我不明白为什么。 – user2099024 2015-02-09 23:04:14

0

编辑我的回答:

要保存与以下行

questionsMutableArray.writeToFile(path!, atomically: true) 

一个数组,但随后试图加载它作为一本字典。您应该将questions数组设置为字典的“Math”键,然后像这样保存字典本身,以使您的读写兼容。

dict["Math"] = questionsMutableArray 
dict.writeToFile(path!, atomically: true) 
+0

Question.plist中的“根”按键是一个包含数组的字典,其中每个数组也包含多个字典。 – user2099024 2015-02-09 23:01:06

+0

你正在阅读来自词典[“数学”]的问题,但是你正在将新的问题添加到不同的数组中,并用该数组替换旧的plist。所以,你的plist文件不再是字典了。您当前的写入和读取不兼容。请检查我已编辑的答案以解决问题 – 2015-02-09 23:13:47

2

首先当你检索plist中,它检索到一个NSMutableDictionary

var dict = NSMutableDictionary(contentsOfFile: path!)! 

然后,一旦你有你的阵列中创建,设置你的字典数学钥匙。 。

dict["math"] = questionsMutableArray 

然后把字典写回你的路径。可能想看看它是否工作,writeToFile返回一个布尔值。

let didItSave = dict.writeToFile(path!, atomically: true) 
+0

这适用于'writeToFile'部分。我可以看到我的新字典保存在'dict [“math”]'中。但是,在我把它写入文件然后重新读取(阅读plist)后,新的字典就不存在了! – user2099024 2015-02-11 13:04:38

+0

请检查我的编辑! – user2099024 2015-02-11 13:21:20

相关问题