gpt4 book ai didi

macos - 使用 NSPipe 配合 NSTask 模拟命令行输入

转载 作者:行者123 更新时间:2023-12-03 17:21:33 37 4
gpt4 key购买 nike

我正在为命令行应用程序编写一个非常简单的 GUI 工具。它只有 2 个按钮。 连接退出
applicationDidFinishLaunching 中,我运行以下命令

NSPipe *pipe = [[NSPipe alloc] init];
writer = [pipe fileHandleForWriting];

NSTask *runTask = [[[NSTask alloc] init] autorelease];
NSString *exefile = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"vpngui"];
[runTask setLaunchPath: exefile];

NSString *exeDir = [[NSBundle mainBundle] resourcePath];
NSArray *pargs;
pargs = [NSArray arrayWithObjects: exeDir, nil];
[runTask setArguments: pargs];
[runTask setStandardInput:pipe];
[runTask launch];

然后,当单击连接按钮时,将运行以下代码行

writer writeData:[@"Connect" dataUsingEncoding:NSUTF8StringEncoding]];

以及退出按钮

writer writeData:[@"Quit" dataUsingEncoding:NSUTF8StringEncoding]];

不知何故,命令行应用程序永远无法获取“连接”和“退出”命令

最佳答案

数据可能已被缓冲。

尝试通过调用 synchronizeFile: 强制刷新文件句柄方法:

synchronizeFile

Causes all in-memory data and attributes of the file represented by the receiver to be written to permanent storage.

关于macos - 使用 NSPipe 配合 NSTask 模拟命令行输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25197583/

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