【发布时间】:2017-05-22 05:02:31
【问题描述】:
所以发生的事情是,当程序试图访问我的 txt 文件的另外 2 行时,它正在爆炸,当我查看数组下的 xcode 时,它显示行由“\t”分隔,但它不会显示其他 2 行,这很令人抓狂……请在下面查看我的代码,感谢您的帮助。
var dictDoc = [String:String]()
var docArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "docs", ofType: "txt")
let fileMgr = FileManager.default
if fileMgr.fileExists(atPath: path!){
do{
let fullText = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
let readings = fullText.components(separatedBy: "\n") as [String]
for i in 1..<readings.count {
let docData = readings[i].components(separatedBy: "\t")
dictDoc["Program"] = "\(docData[0])"
dictDoc["Signature"] = "\(docData[1])"
dictDoc["Extension"] = "\(docData[2])"
docArray.add(dictDoc)
}
}catch let error as NSError{
print("Error: \(error)")
}
self.title = "Word Processor Programs"
}
tableView.dataSource = self
tableView.delegate = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return docArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let doc = docArray[indexPath.row]
cell.textLabel?.text = "\((doc as AnyObject).object(forKey: "Program")!)"
cell.detailTextLabel?.text = "\((doc as AnyObject).object(forKey: "Signature")!) \((doc as AnyObject).object(forKey: "Extension")!)"
return cell
}
@IBAction func Closebtn(_ sender: UIButton) {
}
【问题讨论】:
-
你能发布你试图分解成组件的文本吗?
-
Program Signautre Extension Word 2.0 DB A5 2D 00 DOC MS Pub subheader FD 37 7A 58 5A 00 PUB Acrobat 插件 4D 5A 90 00 03 00 00 00 API MS Pub 58 54 BDR WP dic 43 42 46 49 4C 45 CBD DeskMate Doc 0D 44 4F 43 DOC Perfect Office doc CF 11 E0 A1 B1 1A E1 00 DOC Word doc subheader EC A5 C1 00 DOC MS Off doc D0 CF 11 E0 A1 B1 1A E1 DOC|DOT|PPS|PPT
-
它失去了格式,但这是一个示例
-
您能否以应有的格式将文本添加到您的问题中。
-
我刚刚尝试将字符串分配给操场上的一个变量,它在
CBD DeskMate Doc之前抛出了一个错误unprintable ASCII character found in source file。查看文件中使用的编码。我认为这一定是导致问题的原因。