我有文本格式的数据,该文本格式位于我的应用程序本地文件中。我做了我的研究,但我找不到任何关于它的东西。如何在Swift中读取该文本文件?我应该使用Sqlite或NSBundle的普通文本文件格式?我希望有人能帮助我开始一些事情。
bwleehnv1#
下面将把文件的内容放入字符串中:
let filePath = NSBundle.mainBundle().pathForResource("file", ofType: "txt");let fileData = String(filePath!, encoding:NSUTF8StringEncoding, error:nil)!
let filePath = NSBundle.mainBundle().pathForResource("file", ofType: "txt");
let fileData = String(filePath!, encoding:NSUTF8StringEncoding, error:nil)!
字符串
e0uiprwp2#
var filePath = NSBundle.mainBundle().pathForResource("world_country_state", ofType:"json") var nsMutData = NSData(contentsOfFile:filePath!) var sJson = NSJSONSerialization.JSONObjectWithData(nsMutData!, options: .MutableContainers, error: nil) as! NSMutableDictionary let arraycOuntry: NSArray = sJson.valueForKey("world")?.allKeys as! NSArray let states: NSArray = sJson.valueForKey("world")?.valueForKey("USA") as! NSArraypickerDataSoruce = states.mutableCopy() as! NSMutableArrayIn this Code world_country_state.json is My File just You replace your file name.i think this Code is very helpful for you.
var filePath = NSBundle.mainBundle().pathForResource("world_country_state", ofType:"json")
var nsMutData = NSData(contentsOfFile:filePath!)
var sJson = NSJSONSerialization.JSONObjectWithData(nsMutData!, options: .MutableContainers, error: nil) as! NSMutableDictionary
let arraycOuntry: NSArray = sJson.valueForKey("world")?.allKeys as! NSArray
let states: NSArray = sJson.valueForKey("world")?.valueForKey("USA") as! NSArray
pickerDataSoruce = states.mutableCopy() as! NSMutableArray
In this Code world_country_state.json is My File just You replace your file name.
i think this Code is very helpful for you.
2条答案
按热度按时间bwleehnv1#
下面将把文件的内容放入字符串中:
字符串
e0uiprwp2#
字符串