macos – xcode 6 swift system()命令

macos – xcode 6 swift system()命令,第1张

概述有没有对 swift系统命令的良好描述?例如,这段代码 let x = system("ls -l `which which`") println(x) 产生 -rwxr-xr-x 1根轮14496 8月30日04:29 /usr/bin/which 0 我想将输出与返回代码分开 system()不是Swift命令,而是BSD库函数.你得到了文件 在终端窗口中使用 “man system” 有没有对 swift系统命令的良好描述?例如,这段代码

let x = system("ls -l `which which`")    println(x)

产生
-rwxr-xr-x 1根轮14496 8月30日04:29 /usr/bin/which

0

我想将输出与返回代码分开

解决方法 system()不是Swift命令,而是BSD库函数.你得到了文件
在终端窗口中使用 “man system”:

The system() function hands the argument command to the command
interpreter sh(1). The calling process
waits for the shell to finish executing the command,ignoring SIGINT and SIGQUIT,and blocking SIGCHLD.

“ls”命令的输出只写入标准输出而不是任何
Swift变量.

如果您需要更多控制,那么您必须使用Foundation框架中的NSTask.
这是一个简单的例子:

let task = NSTask()task.launchPath = "/bin/sh"task.arguments = ["-c","ls -l `which which`"]let pipe = NSPipe()task.standardOutput = pipetask.launch()let data = pipe.fileHandleForReading.readDataToEndOffile()if let output = Nsstring(data: data,enCoding: NSUTF8StringEnCoding) {    println(output)}task.waitUntilExit()let status = task.terminationStatusprintln(status)

这里需要通过shell“/ bin / sh -c command …”执行命令,因为
“后退”的论点.通常,最好直接调用命令,
例如:

task.launchPath = "/bin/ls"task.arguments = ["-l","/tmp"]
总结

以上是内存溢出为你收集整理的macos – xcode 6 swift system()命令全部内容,希望文章能够帮你解决macos – xcode 6 swift system()命令所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1077270.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存