objc 文件中有一个宏,如下所示。
#define kSOME_COLOR [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:1.0]
我想从 Swift 访问宏。
self.someView.backgroundColor = kSOME_COLOR
但是我得到了编译错误Use of unresolved identifier 'kNOTE_GREEN_COLOR'
有什么办法吗?
最佳答案
Leo 是对的,你不能像 Swift 中的 Macro 那样拥有 Objective C。您可以在您的 swift 类中重新定义它们。
或者,如果您只想从 objective c 类中使用它们,那么您可以执行以下操作 在您的 .m 文件实现中,并在 header 中公开
+ (UIColor *)k_SOME_COLOR {
return kSOME_COLOR;
}
在.h文件中
+ (UIColor *)k_SOME_COLOR;
在快速文件中
var someColor = Constants.k_SOME_COLOR() // Constants is class name
为此,您需要使用 Swift Bridging header 并将您的常量类包含在桥接 header 中,调用 Objective-C 代码的其余转换只是 Swift 的魔法。 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
关于ios - 如何从 Swift 访问 UIColor 的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31633239/