在Swift3中,我开始使用枚举来描述不同目标的不同行为。
步骤1 - 在目标的plist文件中指定目标类型,Alpha、Beta或Production。
![Indicate the type of build in the target plist](https://istack.dev59.com/fOM5F.webp)
第二步 - 我声明了一个枚举来描述所有可能的构建类型。
enum RedactedAppVersion {
case Alpha
case Beta
case Production
}
第三步 - 我实现了一个懒加载的全局属性来读取plist文件。
lazy var appVersion: RedactedAppVersion = {
if let temp = Bundle.main.object(forInfoDictionaryKey: "RedactedAppVersion") as? String {
if temp.lowercased() == "alpha" {
return RedactedAppVersion.Alpha
}
if temp.lowercased() == "beta" {
return RedactedAppVersion.Beta
}
}
return RedactedAppVersion.Production
}()
Step 4 - 当我需要获取API的路径时,我会查询枚举值...
4a - 将以下内容添加到枚举中。
func apiPath() -> String {
switch self {
case .Alpha: return "https://test.redacted.com/v1/"
case .Beta: return "https://test.redacted.com/v1/"
case .Production: return "https://api.redacted.com/v1/"
}
}
我这么做可以得到正确的路径 -
4b -
,保留HTML标签,不做解释。
let path = ApplicationManagerSingleton.sharedInstance.appVersion.apiPath() + "auth/sign-up"