【发布时间】:2014-12-11 03:59:09
【问题描述】:
我正在尝试合并一个文件浏览器,该浏览器允许用户选择一个文本文件,并将文件的内容显示在 NSTextView 中。我的文件浏览器工作正常,文本框工作,但我似乎无法打开文件并从中生成至少一个或最好是一系列字符串(每行 1 个)。
let FileWindow : NSOpenPanel = NSOpenPanel()
FileWindow.prompt = "Open File"
FileWindow.title = "Select a File"
FileWindow.allowedFileTypes = ["txt"]
FileWindow.canChooseDirectories = false
FileWindow.allowsMultipleSelection = false
FileWindow.runModal()
var selectedfile = FileWindow.URL
var output = selectedfile?.absoluteString
var index: String.Index = advance(output!.startIndex, 7)
FileLabel.stringValue = output!.substringFromIndex(index)
if (selectedfile != nil) {
var data: NSData = NSFileManager.defaultManager().contentsAtPath(output!.substringFromIndex(index))!
var txt: NSString = NSString(data: data, encoding: NSUTF8StringEncoding)!
self.FileDisplayView.textStorage?.mutableString.appendString(txt)
self.FileDisplayView.needsDisplay = true
}
任何帮助将不胜感激!
【问题讨论】:
标签: swift text nstextview