如何我可以用这样的全局常量:斯威夫特使用常量像Objective-C的
#define EXAMPLE 123
从Objective-C的???
我需要使用字符串常量,这些常量必须在AppDelegate中定义,以便在每个类文件中使用#include "AppDelegate.h"
。
如何我可以用这样的全局常量:斯威夫特使用常量像Objective-C的
#define EXAMPLE 123
从Objective-C的???
我需要使用字符串常量,这些常量必须在AppDelegate中定义,以便在每个类文件中使用#include "AppDelegate.h"
。
我建议在一个单独的文件中创建一个结构,其属性为let
常量。因此,像这样
struct myConstants {
static let firstConstant = "hello"
static let secondConstant = "goodbye"
}
谢谢!我会尝试使用它,但我无法将您的答案设置为有用。名誉抵制它。对不起 –
不用担心。乐于帮助。 –
我必须使用#import或#include来查看这个常量我是其他文件吗?在obj-C中,我必须#导入“GlobalConst.swift”才能在其他文件中看到它... –
只需创建一个Struct class
,使每个property/function static
。
struct Global {
static var test = "Some value"
}
叫它Global.test
我不喜欢'struct class'表达式。结构不是类。 –
制作一个恒定的文件,并声明所有常量那里。如果您使用目标c,然后创建一个pch文件并导入该恒定文件,或者如果使用swift,则可以直接访问该常量而不导入任何东西 –