gpt4 book ai didi

swift - 如何在 swift 中修改 shell 环境变量?

转载 作者:行者123 更新时间:2023-11-30 11:26:19 25 4
gpt4 key购买 nike

使用 Process 时如何访问 shell 环境变量之一?如果我使用 environment 来设置它们,它将更改所有环境变量。

let task = Process()
// How do I modify PATH only instead of setting the whole dictionary
task.environment = ["PATH": "/usr/local/bin"]

最佳答案

您可以通过附加到 ProcessInfo.processInfo.environment (继承的环境)您的自定义路径(或您需要的任何路径)来解决它:

let task = Process()
var environment = ProcessInfo.processInfo.environment
environment["PATH"] = "/usr/local/bin"
task.environment = environment
print(task.environment ?? "")

关于swift - 如何在 swift 中修改 shell 环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50707522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com