0
我正在尝试合并一个文件浏览器,允许用户选择一个文本文件,并将文件的内容显示在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
}
任何帮助,将不胜感激!
你不应该使用NSString。只需使用String。 – 2014-12-11 05:00:22
非常感谢所有的信息!我现在只需要阅读这个文件,所以我只是实现了那个,但现在它工作得很好。非常感谢! – user3185748 2014-12-11 13:21:29
不客气 – 2014-12-11 16:38:18