我尝试从我的应用程序启动一个命令文件,所以我需要使用"posix_spawn“。但使用这种方法需要奇怪地使用指针。我没有找到Swift (3.0)的任何例子,只有C++或目标C,我无法翻译。
我只需要这样的东西(在旧的系统调用中):
let err = system("ls -param >file.txt")有什么想法吗?
编辑:链接的解决方案不匹配。首先,它不使用编译器提到的posix_spawn函数。它使用的是NSTask,它似乎也被抛弃了。但我尝试了这个例子,结果是:
func shell(launchPath: String, arguments: [String]) -> String
{
let task = Process()
task.launchPath = launchPath
task.arguments = arguments
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
return output
}这里进行了XCode 8所需的所有更改。但是当调用时,它将永远不会从".launch()“返回。
在输出调试窗口的某个地方,我找到了这一行:
2016-09-15 15:06:36.793 DSRenamer[96562:2569582] launch path not accessible在终端窗口中,相同的命令可以正常工作:
/usr/local/bin/ExifTool -ext .CR2发布于 2016-12-30 03:51:54
我用斯威夫特来称呼目标-c方法(虽然不是最好的方法)。
SystemTaskHelper.performSystemTask("ls -param >file.txt")https://stackoverflow.com/questions/39511050
复制相似问题