ios - 如何从 Swift 访问 UIColor 的宏

标签 ios objective-c swift macros uicolor

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/

相关文章:

objective-c - 实现按住连续事件触发的优雅方式?

ios - UIView 背景颜色设置不是显示的内容

ios - iOS模拟器是虚拟机还是模拟器?

ios - 使用未解析的标识符 'addChild'

ios - UITableView 部分的边框

ios - appInviteDialog didCompleteWithResults 委托(delegate)方法中的 EXC_BREAKPOINT | Facebook 应用程序邀请

ios - Swift:无法使用 NSObjects 将类型 'NSNull' 的值转换为 'NSDictionary'

ios - 拖动时连续发出声音

ios - 在 MKMapView 之上使用 OpenGL?

ios - 显示 iOS 表情符号键盘