我有大约10个Swift 3应用程序。
它们几乎相似,但每个应用程序中都有一些字段会发生变化,我希望这些值可以在完整程序中使用(例如:每个公司的名称,应用程序的主要颜色等) 。这些值在整个程序中将保持不变。
我想通过应用程序创建一个常量文件,因此每个应用程序上使用的值将与其他应用程序不同,因此我不必一直重复每个程序的每个值。例如,如果我设置一个名为company
的常量并将其值更改为company1
,company2
等等...根据文件我可以使用company
常量所有的申请。因此,每次创建新应用程序时,我都不必在整个应用程序中手动替换变量。只需将相应的值替换为每个应用程序。
那么,是否可以创建常量文件?也许添加一个特殊的类。我想也许它有另一个特定的名字,但我找不到它。
提前致谢!
答案 0 :(得分:14)
是的,您可以在Swift中创建Constants
文件。以下是方法:
struct Constants {
//App Constants
static let APP_NAME = "YOUR_APP_NAME"
}
用法:
print(Constants.APP_NAME)
通过创建对象创建一个类和访问权限:
class Constants{
let APP_NAME = "YOUR APP NAME"
}
用法:
let constInstance = Constants()
print(constInstance.APP_NAME)
最有效的方法是使用struct
。
答案 1 :(得分:1)
实际上取决于你想要定义的内容:
例如,如果是app的字体,请声明UIFont
扩展名。通过这种方式,预编译器可以在编写代码时帮助您。
如果有任何特定的常量(字符串,整数等),我会创建一个仅包含Constants.swift
的{{1}}文件。在这种情况下,enums
优于enum
或class
,因为它不能被错误地初始化
枚举常量{ static let appIdentifier:String =“string” }
它只能以这种方式使用:struct
它不能通过执行Constants.appIdentifier
来初始化(编译器会抛出错误)。
有关详细信息,请参阅https://www.natashatherobot.com/swift-enum-no-cases/
答案 2 :(得分:1)
最简洁的方法(遵循文件方法)是使用$_REQUEST
文件并为每个变量集构建该段用例。例如:
Constants.swift
简单地使用:
class Constants {
struct Validation {
static let InvalidValue = "n/a"
static let IncompleteValue = "Incomplete"
}
struct Labels {
static let Username = "Username"
}
}
这为您提供了更易读的变量以及有组织的和集中的常量文件。如果文件太大,您可以选择使用相同的方法将其拆分为多个文件。
答案 3 :(得分:0)
使用Structure
定义Constant
。
执行如下操作。
struct Company {
static let kCountry1 = "Company1"
static let kCountry2 = "Company2"
static let kCountry3 = "Company3"
static let kCountry4 = "Company4"
}
像这样使用。
print(Company.kCountry1)
答案 4 :(得分:0)
在swift中使用let
创建常量。只需创建一个Constants.swift
文件并定义常量:
let mainColor = UIColor.green
您可能希望单独对其进行版本设置,并可能将其添加到自定义框架中。
答案 5 :(得分:0)
这是一个稍微不同的解决方案,不使用常量文件,而是使用plist的单例类。这允许您在所有应用程序中使用一个设置文件,然后每个应用程序只需要一个plist文件。
如果您在项目中添加一个名为" CustomSettings.plist"具有以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PrimaryColor</key>
<string>cc3333</string>
<key>CompanyName</key>
<string>Company1</string>
</dict>
</plist>
然后使用以下代码创建一个名为Settings的新文件:
public class Settings {
static let instance = Settings()
public let companyName: String
public let primaryColor: UIColor
private init() {
let path = Bundle.main.path(forResource: "CustomSettings", ofType: "plist")!
let settingsDict = NSDictionary(contentsOfFile: path) as! [String: AnyObject]
companyName = settingsDict["CompanyName"] as! String
let colorString = settingsDict["PrimaryColor"] as! String
primaryColor = Settings.hexStringToUIColor(hex: colorString)
}
// Taken from: http://stackoverflow.com/questions/24263007/how-to-use-hex-colour-values-in-swift-ios
private static func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.characters.count) != 6) {
return UIColor.gray
}
var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
}
现在您只需要一个框架中可以拥有的设置文件,然后每个应用程序只需要包含自定义内容的CustomSettings plist文件。您可以按如下方式使用它:
print (Settings.instance.companyName)
view.backgroundColor = Settings.instance.primaryColor