如何在Swift中引用环境变量? [重复]

时间:2022-08-04 23:25:57

This question already has an answer here:

这个问题在这里已有答案:

I'm pretty new to Swift and Xcode so please forgive my ignorance. I am trying to set up an environment variable to store my API keys since I am planning on pushing my project to GitHub and don't want the API in a public repo. I read that using environment variables is a good way to do this. I went into Xcode and set up a scheme that included an environment variable like this:

我对Swift和Xcode很新,所以请原谅我的无知。我正在尝试设置一个环境变量来存储我的API密钥,因为我计划将我的项目推送到GitHub并且不希望API在公共仓库中。我读到使用环境变量是一种很好的方法。我进入Xcode并设置了一个包含这样的环境变量的方案:

environment variable

how do I actually reference it in Swift? I tried just putting "GMAPS_API" in the code where my API key should go, but it's not recognizing it. Am I even doing this correctly? I have tried to look online, but I am still confused. Any help or pointers in the right direction would be great. Thanks

我如何在Swift中实际引用它?我试过把“GMAPS_API”放在我的API密钥应该去的代码中,但是它没有认识到它。我甚至做得对吗?我试图在网上看,但我仍然感到困惑。任何正确方向的帮助或指针都会很棒。谢谢

1 个解决方案

#1


6  

@OOper pointed me in the right direciton. All I had to do was:

@OOper指出我在正确的方向。我所要做的就是:

let apiKey = ProcessInfo.processInfo.environment["GMAPS_API"]

#1


6  

@OOper pointed me in the right direciton. All I had to do was:

@OOper指出我在正确的方向。我所要做的就是:

let apiKey = ProcessInfo.processInfo.environment["GMAPS_API"]