How to use Swift struct in Objective-C

前端 未结 4 789
甜味超标
甜味超标 2020-11-28 21:07

Simply I have a struct that stores the application constants as below:

struct Constant {

    static let ParseApplicationId = \"xxx\"
    static let ParseCli         


        
4条回答
  •  一生所求
    2020-11-28 21:37

    Sad to say, you can not expose struct, nor global variables to Objective-C. see the documentation.

    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 }
    }
    

    In Objective-C, you can use them like this:

    NSString *appklicationId = [Constant parseApplicationId];
    NSString *clientKey = [Constant parseClientKey];
    UIColor *greenColor = [Constant appGreenColor];
    

提交回复
热议问题