简单地说,我有一个存储应用程序常量的结构,如下所示:
struct Constant {
static let ParseApplicationId = "xxx"
static let ParseClientKey = "xxx"
static var AppGreenColor: UIColor {
return UIColor(hexString: "67B632")
}
}
Constant.ParseClientKey
例如,可以通过调用在Swift代码中使用这些常量。但是在我的代码中,它还包含一些Objective-C类。所以我的问题是如何在Objective-C代码中使用这些常量?
如果这种声明常量的方法不好,那么在Swift和Objective-C代码中使用全局常量的最佳方法是什么?
可悲的是,你不能暴露struct
,也不是Objective-C的全局变量。请参阅文档。
到目前为止,恕我直言,最好的方法是这样的:
let ParseApplicationId = "xxx"
let ParseClientKey = "xxx"
let AppGreenColor = UIColor(red: 0.2, green: 0.7, blue: 0.3 alpha: 1.0)
@objc class Constant: NSObject {
private init() {}
class func parseApplicationId() -> String { return ParseApplicationId }
class func parseClientKey() -> String { return ParseClientKey }
class func appGreenColor() -> UIColor { return AppGreenColor }
}
在Objective-C中,您可以像这样使用它们:
NSString *appklicationId = [Constant parseApplicationId];
NSString *clientKey = [Constant parseClientKey];
UIColor *greenColor = [Constant appGreenColor];
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句