【问题标题】:Swift environment variable returns nil when building app构建应用程序时,Swift 环境变量返回 nil
【发布时间】:2018-12-13 00:24:06
【问题描述】:

说明

我的环境变量有问题。当我构建我的应用程序并运行时,一切正常,但是当我按下“停止”或将其归档到应用商店时,环境变量返回 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 repo 上问题的最小再现 https://github.com/MasterBroki/test-environment-variable

感谢您的回答!

【问题讨论】:

    标签: swift environment-variables tvos


    【解决方案1】:

    遇到同样的问题。使用带有检查的普通调试和发布配置选项,例如

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

    为我工作

    【讨论】:

      【解决方案2】:

      Xcode 将环境变量从方案传递到 iOS 运行时。当您在 Xcode 之外运行应用程序时,它们不会被传递。我也遇到过这个限制,你可以找到类似的问题here

      此方法的替代方法是使用配置(例如调试、发布或您为特定目的而制作的配置)和“预处理器标志”或“其他 Swift 标志”的配对。您可以在here 找到有关此方法的一些指导。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-15
        • 2020-09-04
        • 2017-01-30
        • 2021-11-03
        • 2014-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多