我写了一个 Mac + iOS 库,它依赖于自动合成的属性。我让某人尝试在 32 位下编译它,忽略一堆编译器警告,并在运行时得到无法识别的选择器。
如果不实现一堆 getter 和 setter,所有代码都无法工作,所以我宁愿用 #error
来停止它们。
我以为我可以做到这一点:
#if !__has_feature(objc_default_synthesize_properties)
#error This library requires the modern runtime and will not compile under 32-bit
#endif
但是没有效果。
为了得到我想要的结果,我必须这样做:
#if !__has_feature(objc_default_synthesize_properties) || defined(__i386__)
#error This library requires the modern runtime and will not compile under 32-bit
#endif
我知道除了 32 位 Intel 架构之外还有其他实例会导致问题,例如旧版本的 Mac OS。
是否有更好的宏来检查自动合成属性或现代运行时的可用性?
最佳答案
因为现代运行时从 Mac OS X 10.5 开始可用,如果给定系统支持现代运行时,宏 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
应该正确评估。可能还需要检查 i386
架构。以上内容也可以用 NSAppKitVersionNumber10_5
在 iOS 上不需要检查,因为每个 iOS 设备都支持现代 Objective-C 运行时,因此也支持变量合成。
这里有一些宏也包含了 Steven Fisher 的回答。它们应该在任一平台上工作,并检查现代编译器和现代运行时:
#if !( defined(__clang__) && __has_feature(objc_default_synthesize_properties) && \
( TARGET_OS_IPHONE || \
( NSAppKitVersionNumber10_5 && !defined(__i386__) ) ) )
#error This library requires autosynthesized properties
#endif
关于objective-c - 当现代运行时或自动合成的属性不可用时中止编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15457022/