Bir*_*chi 2 unix objective-c system-calls
是否可以在Objective-C中进行系统调用?
我有以下代码:
if (!system("ls -l")) {
NSLog(@"Successfully executed");
} else {
NSLog(@"Error while executing the command");
}
Run Code Online (Sandbox Code Playgroud)
如何获得输出?
谢谢
这是使用-[NSFileManager contentsOfDirectoryAtPath:error:]而不是包装内置shell函数和解析输出的完美候选者.对于需要输出的通用命令 - 以及Cocoa中没有"免费"的等效功能(只需花时间学习可用的API) - NSTask通常是一个更好的选择system().