Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Xcode:Swift-如何根据执行环境声明具有不同值的变量/常量?_Ios_Xcode_Debugging_Swift_Release - Fatal编程技术网

Ios Xcode:Swift-如何根据执行环境声明具有不同值的变量/常量?

Ios Xcode:Swift-如何根据执行环境声明具有不同值的变量/常量?,ios,xcode,debugging,swift,release,Ios,Xcode,Debugging,Swift,Release,我试图找出如何处理不同环境下的变量/常量,例如开发(或调试)和发布。例如,在执行单元测试时,web服务的url应该指向本地主机,但在最终产品中,它应该指向公共api主机 我读过一些关于将Swift编译器-自定义标志调试设置设置为-DDEBUG的内容,然后在代码中声明变量,如下所示: #if DEBUG let url = "http://localhost" #else let url = "https://api.example.com" #endif 但那没用。运行单元测试时,ur

我试图找出如何处理不同环境下的变量/常量,例如开发(或调试)和发布。例如,在执行单元测试时,web服务的url应该指向本地主机,但在最终产品中,它应该指向公共api主机

我读过一些关于将Swift编译器-自定义标志调试设置设置为
-DDEBUG
的内容,然后在代码中声明变量,如下所示:

#if DEBUG
  let url = "http://localhost"
#else
  let url = "https://api.example.com"
#endif
但那没用。运行单元测试时,url从未设置为
http://localhost
。我错过了什么吗?

编辑项目方案

定义环境变量:

最后检查是否为正在处理的架构定义了:

var baseURL:String{
    get{
        if let _ = ProcessInfo().environment["LOCAL_MOCK_SERVER"]{
            return "http:/localhost:3000"
        } else{
            return "https://api.fixer.io"
        }

    }
}

请看这篇文章将帮助你[链接][1][1]:谢谢,现在工作。解决方案是带有空格的
-D DEBUG