Cocoa/Objective-C Shell 命令行执行 [英] Cocoa/ Objective-C Shell Command Line Execution
本文介绍了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屋!
查看全文