在Swift IOS中读取本地文本文件

zlhcx6iw  于 2024-01-05  发布在  Swift
关注(0)|答案(2)|浏览(160)

我有文本格式的数据,该文本格式位于我的应用程序本地文件中。我做了我的研究,但我找不到任何关于它的东西。如何在Swift中读取该文本文件?我应该使用Sqlite或NSBundle的普通文本文件格式?我希望有人能帮助我开始一些事情。

bwleehnv

bwleehnv1#

下面将把文件的内容放入字符串中:

  1. let filePath = NSBundle.mainBundle().pathForResource("file", ofType: "txt");
  2. let fileData = String(filePath!, encoding:NSUTF8StringEncoding, error:nil)!

字符串

e0uiprwp

e0uiprwp2#

  1. var filePath = NSBundle.mainBundle().pathForResource("world_country_state", ofType:"json")
  2. var nsMutData = NSData(contentsOfFile:filePath!)
  3. var sJson = NSJSONSerialization.JSONObjectWithData(nsMutData!, options: .MutableContainers, error: nil) as! NSMutableDictionary
  4. let arraycOuntry: NSArray = sJson.valueForKey("world")?.allKeys as! NSArray
  5. let states: NSArray = sJson.valueForKey("world")?.valueForKey("USA") as! NSArray
  6. pickerDataSoruce = states.mutableCopy() as! NSMutableArray
  7. In this Code world_country_state.json is My File just You replace your file name.
  8. i think this Code is very helpful for you.

字符串

相关问题