Cocoa / Objective-C Shell命令行执行 [英] Cocoa/ Objective-C Shell Command Line Execution

查看:403
本文介绍了Cocoa / Objective-C Shell命令行执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是如何从我的Cocoa应用程序执行shell命令?

This is probably a stupid question, but how can I execute a shell command from my Cocoa app?

我有一个命令作为字符串命令可以根据需要轻松处理数据。

I have the command as a string "command", but can easily manipulate data as needed.

无需获取返回的输出值。

There is no need to get a returned output value.

推荐答案

NSTask 很漂亮容易做到这一点。对于同步调用,您可以使用类似以下片段:

NSTask is pretty easy to do this with. For a synchronous call, you can use something like this fragment:

NSString *path = @"/path/to/executable";
NSArray *args = [NSArray arrayWithObjects:..., nil];
[[NSTask launchedTaskWithLaunchPath:path arguments:args] waitUntilExit];

-waitUntilExit 调用确保完成然后继续。如果任务可以是异步的,你可以删除该调用,并让NSTask做它的事情。

The -waitUntilExit call makes sure it finishes before proceeding. If the task can be asynchronous, you can remove that call and just let the NSTask do it's thing.

这篇关于Cocoa / Objective-C Shell命令行执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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