Swift 环境变量 returns 构建应用程序时为零

Swift environment variable returns nil when building app

描述

我对环境变量有疑问。当我构建我的应用程序并且它是 运行 时,一切正常,但是当我按 "stop" 或我将其存档到应用程序商店时,环境变量 returns nil(或空字符串,我我还不太确定)。

重现方式:

  1. 构建应用程序
  2. 运行它在模拟器上("Hello world"会出现)
  3. 停止应用程序
  4. 在模拟器中,返回应用程序("Hello word"不会出现)

最小复制:

class ViewController: UIViewController {
    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        label.text = ProcessInfo.processInfo.environment["testVariable"]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

Outlet 标签引用了一个简单的故事板

storyboard screenshot

这是我的环境变量的配置

Variables configuration

最后,这里是 github 回购上问题的最小再现 https://github.com/MasterBroki/test-environment-variable

感谢您的回答!

Xcode 将环境变量从方案传递到 iOS 运行 时间。当您 运行 在 Xcode 之外申请时,它们不会通过。我也运行进入这个限制,你可以找到一个类似的问题here

此方法的替代方法是使用成对的配置(例如调试、发布或您为特定目的制作的配置)和 "Preprocessor Flags" 或 "Other Swift Flags." 您可以找到一些指导这种方法 .

运行成同样的问题。使用正常的 Debug 和 Release 配置选项进行检查,例如

#if DEBUG
  <logic>
#else
  <logic>
#endif

适合我