更新时间:2022-12-04 10:54:09
很遗憾,你不能将 struct
和全局变量暴露给 Objective-C.请参阅文档,其中部分说明:
Sad to say, you can not expose struct
, nor global variables to Objective-C. see the documentation, which states in part:
在需要 Objective-C 互操作性时使用类
Use Classes When You Need Objective-C Interoperability
如果您使用需要处理数据的 Objective-C API,或者您需要将数据模型适合在 Objective-C 框架中定义的现有类层次结构中,您可能需要使用类和类继承来建模你的数据.例如,许多 Objective-C 框架公开了您希望子类化的类.
If you use an Objective-C API that needs to process your data, or you need to fit your data model into an existing class hierarchy defined in an Objective-C framework, you might need to use classes and class inheritance to model your data. For example, many Objective-C frameworks expose classes that you are expected to subclass.
到目前为止,恕我直言,***的方法是这样的:
As of now, IMHO, the best way is something like this:
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 中,你可以像这样使用它们:
In Objective-C, you can use them like this:
NSString *appklicationId = [Constant parseApplicationId];
NSString *clientKey = [Constant parseClientKey];
UIColor *greenColor = [Constant appGreenColor];