gpt4 book ai didi

cocoa - 运行 a 命令时将 `stdout` 放入 `NSTextView` 中

转载 作者:行者123 更新时间:2023-12-03 17:08:10 25 4
gpt4 key购买 nike

我正在创建一个 Cocoa 应用程序,它需要运行 rails 命令。此命令生成一个输出,并将其流式传输到 stdout。我想在 NSTextView 中向用户显示此输出(因此基本上将 stdout 流式传输到 NSTextView)。当命令运行时,我的应用程序不应“挂起”(例如,git 命令需要很长时间才能完成上传)。我并不真正关心应该如何运行命令,只要我可以设置工作目录即可。

对 Cocoa 使用 Ruby 框架不是一个选择,因为我还需要对 git 等非 ruby​​ 命令执行此操作。

有人可以帮助我吗?谢谢

最佳答案

您将需要使用NSTask类。

NSTask       * task;
NSPipe * pipe;
NSFileHandle * fileHandle;

task = [ [ NSTask alloc ] init ];
pipe = [ NSPipe pipe ];
fileHandle = [ pipe fileHandleForReading ];

[ fileHandle readInBackgroundAndNotify ];
[ task setLaunchPath: @"/bin/ls" ];
[ task setStandardOutput: pipe ];
[ task setStandardError: pipe ];
[ task launch ];

然后您可以使用文件句柄来获取标准输出。
可以使用 setCurrentDirectoryPath 方法设置工作目录。
可以使用 setArguments 方法设置参数。

关于cocoa - 运行 a 命令时将 `stdout` 放入 `NSTextView` 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2704295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com