在 Swift IOS 中读取本地文本文件
Read Local Text File in Swift IOS
我有一个文本格式的数据,该文本格式位于我的应用本地文件中。我做了我的研究,但我找不到任何关于它的信息。如何读取 swift 中的文本文件。我应该在 NSBundle 中使用 Sqlite 还是普通文本文件格式。我希望有人能帮助我开始。谢谢。
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.
下面将文件内容放入字符串中:
let filePath = NSBundle.mainBundle().pathForResource("file", ofType: "txt");
let fileData = String(filePath!, encoding:NSUTF8StringEncoding, error:nil)!
我有一个文本格式的数据,该文本格式位于我的应用本地文件中。我做了我的研究,但我找不到任何关于它的信息。如何读取 swift 中的文本文件。我应该在 NSBundle 中使用 Sqlite 还是普通文本文件格式。我希望有人能帮助我开始。谢谢。
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.
下面将文件内容放入字符串中:
let filePath = NSBundle.mainBundle().pathForResource("file", ofType: "txt");
let fileData = String(filePath!, encoding:NSUTF8StringEncoding, error:nil)!