How to use Swift struct in Objective-C

前端 未结 4 799
甜味超标
甜味超标 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:44

    You should make the let statements private if you want to make other Swift types in your code to access these constants only via class:

    private let AppGreenColor = UIColor(red: 0.2, green: 0.7, blue: 0.3 alpha: 1.0)
    
    @objc class Constant {
        class func appGreenColor() -> UIColor { return AppGreenColor }
    }
    

    In Swift, you can use them like this:

    UIColor *greenColor = Constant.appGreenColor
    

    The following line will not compile anymore now since the let statement is private:

    UIColor *greenColor = appGreenColor
    

提交回复
热议问题