是否可以在Swift上创建常量文件? [英] Is it possible to create a constants file on Swift?
问题描述
我大约有10个Swift 3应用程序.
I have around 10 Swift 3 applications.
它们几乎相似,但是每个应用程序中都有一些字段会发生变化,我希望这些值可以在整个程序中使用(例如:每个公司的名称,应用程序的原色等) .这些值在整个程序中将是恒定的.
They are almost similar but there are some fields that changes in each application and I would like that these values could be used in the entirely program (for example: the name of each company, the primary colour on the application, etc). These values will be constant in the whole program.
我想按应用程序创建一个常量文件,因此将在每个应用程序上使用的值将与其他应用程序不同,因此我将不必一直在每个程序上重复每个值.例如,如果我设置一个名为company
的常量并将其值更改为company1
,company2
等...,具体取决于文件,则可以在所有应用程序中使用该company
常量.因此,我不必在每次创建新应用程序时都在整个应用程序中手动替换该变量.只需替换每个应用程序的相应值即可.
I would like to create one constants file by application so the values that will be used on each application will be different from the others so I will not have to repeat on each program each value all the time. For example, if I set a constant named company
and change its value to company1
, company2
, etc... depending on the file I can use that company
constant in all the applications. So I will not have to replace the variable manually in the whole application each time I create a new one. Just replacing the corresponding values to each application.
那么,可以创建一个常量文件吗?也许添加一个特殊的类.我想也许它有另一个特定名称,但我找不到它.
So, is it possible to create a constants file? Maybe adding a special class. I guess that maybe it has another specific name but I could not find it.
提前谢谢!
推荐答案
是的,您可以在Swift中创建Constants
文件.下面是方法.
Yes you can create Constants
file in Swift. Below are the ways.:
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
.
这篇关于是否可以在Swift上创建常量文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!