中的NSScrollView和NSTextView中显示控制台输

中的NSScrollView和NSTextView中显示控制台输

本文介绍了在Swift中的NSScrollView和NSTextView中显示控制台输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几天我一直在学习Swift,但我似乎遇到了麻烦.

I've been learning Swift the past couple days, and I seem to have hit a rock.

我正在尝试创建一个ViewController窗口,其中有一个按钮和一个NSViewText.当我按下按钮时,应用程序会向用户提示带有NSTextField的工作表.按下Enter键后,NSTextField字符串将保存在变量中,并且工作表将关闭,并执行终端命令.

I am trying to create a ViewController window where I have a button and an NSViewText. When I push the button, the app prompts the user with a sheet that has an NSTextField. When pressed enter, the NSTextField string is saved in a variable and the sheet is closed, and a terminal command is executed.

现在,我在AppDelegate中的execute命令方法使用NSPipe创建了一个管道:

Now, my execute command method in AppDelegate creates a pipe with NSPipe:

let cmd = NSTask()
...
let pipe = NSPipe()
cmd.standardOutput = pipe
cmd.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)!

var consoleOutput = output
return output

consoleOutput在execute方法之外,所以我试图在ViewController中显示它,如下所示:

consoleOutput is outside the execute method, so I'm trying to display it in ViewController, like this:

更改了以前的代码,但仍然出现错误:

textView.string = myDelegate.consoleOutput

我希望终端输出实时显示在NSViewText(或NSTextFieldNSScrollViewNSClipView?不确定)中.我无法做的是找到一种操作NSTextView内容的方法,因为当我运行它时,会出现以下错误:

I want the terminal output to be shown within the NSViewText (or NSTextField, or NSScrollView, NSClipView? not sure which), real time. What I cannot do is find a way to manipulate the NSTextView's content, because when I run it, I get the following error:

fatal error: unexpectedly found nil while unwrapping an Optional value

任何帮助将不胜感激.

推荐答案

如果完全可以分配闭包(例如代码块),则必须将其放在声明中的'='后面,而不是:在LHS上.您基本上是在尝试将类型声明为静态代码块.我很惊讶编译器使您摆脱了所有抱怨IBOutlet方面本身的问题.

If assigning a closure (e.g. code block) would work at all, you'd have to put it after an '=' in the declaration, not after the : on the LHS. You're basically trying to declare the type as a static block of code. I'm surprised the compiler it lets you get away with that all complaining only about the IBOutlet aspect itself.

这篇关于在Swift中的NSScrollView和NSTextView中显示控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:16