NSTask和NSPipe示例与命令行objective-c通信 [英] NSTask and NSPipe example to comunicate with the command line objective-c

查看:145
本文介绍了NSTask和NSPipe示例与命令行objective-c通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以举一个简单的例子来说明如何结合使用 NSTask 和 NSPipe 来做到这一点:

Can someone show a quick example on how to use NSTask and NSPipe in conjunction to do this:

Charlie AI - 通过终端运行与 AI 通信

Charlie AI - run through terminal to comunicate with the AI

我想使用 xcode 和 Objective c 为它创建一个漂亮的 GUI.我想有 2 个 NSTextFields 用于 charlie 的响应和用户输入.然后有一个发送按钮将用户输入发送到命令行,然后获取查理的响应并将其发布到 NSTextField 中.

I want to create a nice GUI for it using xcode and objective c. I want to have 2 NSTextFields for the charlie's response and user input. Then have a send button to send the user input to the command line, then get the charlie's response and post it in the NSTextField.

我可以处理 GUI 的东西(NSTextField 等),但我需要 Objective-c 编码部分的帮助.

I can handle the GUI stuff (NSTextField, ect.) But I need help with the objective-c coding part.

谢谢!

以利亚

推荐答案

Apple 有一些很好的示例代码,展示了如何完成大部分工作.

Apple has some nice sample code that shows how to do most of that.

TaskWrapper.m 包含所有聪明的东西,但是由于您希望能够向任务发送数据,您需要对其进行一些扩展,如下所示:

TaskWrapper.m contains all the clever stuff, but since you want to be able to send data to the task, you'll need to extend it a little, like so:

[task setStandardInput: [NSPipe pipe]];

要向任务发送输入,您可以执行以下操作:

To send input to the task, you can then do:

[[[task standardInput] fileHandleForWriting] writeData: ...];

要将 NSTextField 的值转换为数据,您可以执行以下操作:

To turn the NSTextField's value into data, you can do something like this:

NSData *data = [[inputTextField stringValue] 
                dataUsingEncoding:NSUTF8StringEncoding];

...并为您的子任务设置当前工作目录,请使用[NSTask setCurrentDirectoryPath:]

...and to set the current working directory for your sub-task, use [NSTask setCurrentDirectoryPath:]

例如

[task setCurrentDirectoryPath: @"/blah/blah"];
[task setLaunchPath: @"/blah/blah/server.sh"];

.... (other setup code)

[task launch];

这篇关于NSTask和NSPipe示例与命令行objective-c通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆