正如 Kevin 所指出的,如果您在静态库项目中的任何时候使用 ARC,它只会与 LLVM Compiler 3.0 及更高版本兼容。
但是,如果您想创建一个使用手动引用计数但又可在启用 ARC 的项目中使用并且与旧编译器兼容的框架,您可能需要设置一些编译器定义。我们必须为 Core Plot 框架执行此操作,以使该框架的标头在 ARC 和使用各种编译器和目标构建的非 ARC 项目之间兼容。
为此,我借鉴了 Ryan Petrich 的 ZWRCompatibility,他在回答 here 中对此进行了描述,并组装了以下定义:
#if TARGET_OS_IPHONE && defined(__IPHONE_5_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0) && __clang__ && (__clang_major__ >= 3)
#define CPT_SDK_SUPPORTS_WEAK 1
#elif TARGET_OS_MAC && defined(__MAC_10_7) && (MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_7) && __clang__ && (__clang_major__ >= 3)
#define CPT_SDK_SUPPORTS_WEAK 1
#else
#define CPT_SDK_SUPPORTS_WEAK 0
#endif
#if CPT_SDK_SUPPORTS_WEAK
#define __cpt_weak __weak
#define cpt_weak_property weak
#else
#if __clang__ && (__clang_major__ >= 3)
#define __cpt_weak __unsafe_unretained
#else
#define __cpt_weak
#endif
#define cpt_weak_property assign
#endif
这让您可以引用非保留(分配)的实例变量,如下所示:
__cpt_weak CPTAnnotationHostLayer *annotationHostLayer;
与
的匹配属性定义
@property (nonatomic, readwrite, cpt_weak_property) __cpt_weak CPTAnnotationHostLayer *annotationHostLayer;
对于使用 LLVM 编译器 3.0 的 iOS 5.0 和 Lion 的目标,这使得这些属性使用更安全的 __weak 限定符。对于 LLVM Compiler 3.0 下的 iOS 4.0 和 Snow Leopard,这将变为 __unsafe_unretained。最后,对于任何其他编译器,限定符为空且属性设置为assign。
retain 可用于所有编译器中没有太大问题的属性。