我正在开发一个应用程序,它将会:
- 检查互联网连接;
- 从服务器接收JSON文件,并在可访问的情况下将数据存储在文件中;
- 如果无法连接到服务器,则从文件中读取。
现在,我已经成功实现了连接检查、接收JSON和将数据放入数组的功能。
接收到的JSON文件的格式如下:
然后我使用以下代码创建一个字典数组来存储JSON数据。选择字典数组是为了能够对其进行过滤,然后使用值的键来访问值。
var rates = [ExchangeRates]() //instance of class Rate
var bnkDct = ["bank": "", "currency": "","buyrate": "", "sellrate": ""] //template
var indx : Int = 0 //index for iteration
for rate in jsonData{
let rate = ExchangeRates(data: rate as! NSDictionary)
rates.append(rate)
bnkDct["bank"] = rates[indx].bank
bnkDct["buyrate"] = rates[indx].buyRate
bnkDct["sellrate"] = rates[indx].sellRate
bnkDct["currency"] = rates[indx].currency
self.bankDict.append(bnkDct)
indx += 1
}
在此之后,我有一个看起来像这样的数组: 现在我想将这个数组保存到文件中,并且显然要能够读取它。
我尝试使用类似于以下代码来编写数据:
let filePath = NSTemporaryDirectory() + "MyData.dat"
self.bankDict.writeToFile(filePath, automatically: true)
但是它给了我一个错误:"[(Dictionary)] doesn't have writeToFile member"。
而且我无法将其转换为NSDictionary来使用相同的writeToFile方法。
另外,我试图使用以下方式写入数据:
let filePath = NSTemporaryDirectory() + "MyData.dat"
NSKeyedArchiver.archiveRootObject(self.bankDict, toFile: filePath)
它创建了一个具有奇怪编码的文件:
当我尝试阅读它时:
let newDictionary = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as! [Dictionary <String,AnyObject> ]
我收到了这个错误提示:"fatal error: unexpectedly found nil while unwrapping an Optional value"。
同时,调试器显示了一个非常"有趣"的东西:
newDictionary变量有无数个值。在文件中编写的数组中有40个字典。
有人能给我建议吗?我在这里做错了什么,或者我在做某些事情时使用了不好的实践?
as! [Dictionary <String,AnyObject> ]
,然后查看该值是什么。 - AdamNSTemporaryDirectory
路径每次应用程序启动时都会发生变化。解决方法是将其保存到永久位置。至于newDictionary
和它的无数项,这只是调试器因为在变量声明之后但在填充之前停止时找不到实际变量内容而感到困惑。我敢打赌这个变量实际上是空的。 - Eric Ayalet filePath = getFileURL("MyData.dat").path!
我尝试比较文件路径,它们是匹配的。 - Almazini[String:String]
的数组可以写入一个属性列表文件,该文件的扩展名为plist
。 - vadian