Swift globals and global functions in objective c

前端 未结 2 624
盖世英雄少女心
盖世英雄少女心 2020-12-08 14:39

the documentation says:

Global constants defined in C and Objective-C source files are automatically imported by the Swift compiler as Swift global

2条回答
  •  [愿得一人]
    2020-12-08 15:11

    Nice answer by @rintaro, but another alternative simple Swift answer for constants that can be used in both Swift and Objective-C:

    @objcMembers
    class MyConstants: NSObject {
        static let kMyConstant1 = "ConstantValue1";
        static let kMyConstant2 = "ConstantValue2";
        static let CARDS = ["card1", "card2"]
    }
    

    You can access this on both Swift and Objective-C by:

    MyConstants.kMyConstant1 // this will return "ConstantValue1"
    MyConstants.CARDS // this will return array ["card1", "card2"]
    

提交回复
热议问题