阅读斯威夫特按行的文本文件行? [英] Read a text file line by line in Swift?
本文介绍了阅读斯威夫特按行的文本文件行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
刚刚开始学习斯威夫特,我有我的code从文本文件阅读,和App显示整个文本文件的内容。我如何通过线显示行并在该行多次调用?
Just started learning Swift, I have got my code to read from the text file, and the App displays the content of the Entire Text file. How can I display line by line and call upon that line multiple times?
TextFile.txt的含有以下内容:
TextFile.txt contains the following.
- 香蕉
- 苹果
- 梨
- 草莓
- 蓝莓
- 黑茶
以下是目前有..
if let path = NSBundle.mainBundle().pathForResource("TextFile", ofType: "txt"){
var data = String(contentsOfFile:path, encoding: NSUTF8StringEncoding, error: nil)
if let content = (data){
TextView.text = content
}
此外,如果有另一种方式,这样做的请告诉我。大部分AP preciated
also if there is another way, of doing this please let me know. Much appreciated
推荐答案
您可以试试:
if let path = NSBundle.mainBundle().pathForResource("TextFile", ofType: "txt"){
var data = String(contentsOfFile:path, encoding: NSUTF8StringEncoding, error: nil)
if let content = data {
let myStrings = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
TextView.text = myStrings[0]
}
}
变量 myStrings
应该是每一行数据。
所使用的code是:
通过线的iOS SDK文件读取的行写在Obj- C和使用的NSString
The code used is from: Reading file line by line in iOS SDK written in Obj-C and using NSString
这篇关于阅读斯威夫特按行的文本文件行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文