简体   繁体   English

在使用LLVM-GCC编译ARC文件时获取“在'strong'之前预期属性属性”

[英]Getting “Expected a property attribute before 'strong'” when compiling an ARC file with LLVM-GCC

I have a project that contains some files that are using ARC, and some are not. 我有一个项目,其中包含一些使用ARC的文件,有些则没有。 The ones that are not have the compiler flag that disables ARC. 那些没有禁用ARC的编译器标志的那些。 That works fine. 这很好。

I also want to make sure my library compiles for LLVM-GCC and also LLVM Compiler. 我还想确保我的库编译LLVM-GCC和LLVM编译器。

I have a property like this: 我有这样的房产:

@property (strong, nonatomic) NSString *foo;

However, when I compile in LLVM-GCC, I get: 但是,当我在LLVM-GCC中编译时,我得到:

"Expected a property attribute before 'strong'" “在'强'之前预期属性属性”

If I change strong to retain, it compiles fine. 如果我改变强势保留,它编译好。 Strong also works fine in LLVM Compiler. Strong在LLVM编译器中也可以正常工作。

What am I missing? 我错过了什么? Isn't strong synonymous with retain? 保留不是强有力的代名词吗?

You're using an obsolete compiler and expecting it to support new language features. 您正在使用过时的编译器并期望它支持新的语言功能。 That simply isn't going to happen. 这根本就不会发生。 If you're using keywords introduced with ARC, you need to be using Clang. 如果您使用ARC引入的关键字,则需要使用Clang。

As Kevin indicates, if you're using ARC at any point in your static library project, it will only be compatible with the LLVM Compiler 3.0 and up. 正如Kevin指出的那样,如果您在静态库项目中的任何位置使用ARC,它将只与LLVM Compiler 3.0及更高版本兼容。

However, if you want to create a framework that uses manual reference counting, yet is usable within ARC-enabled project and is compatible with older compilers, you might need to set up some compiler defines. 但是,如果要创建使用手动引用计数的框架,但在启用ARC的项目中可以使用并且与较旧的编译器兼容,则可能需要设置一些编译器定义。 We had to do this for the Core Plot framework in order to make the headers for that framework compatible across ARC and non-ARC projects built using a variety of compilers and targets. 我们必须为Core Plot框架执行此操作,以使该框架的标头与使用各种编译器和目标构建的ARC和非ARC项目兼容。

To do this, I drew from Ryan Petrich's ZWRCompatibility , which he describes in his answer here , and assembled the following defines: 为此,我从Ryan Petrich的ZWRCompatibility中得出了他在这里的回答中所描述的,并汇总了以下定义:

#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

This lets you refer to non-retained (assigned) instance variables like the following: 这使您可以引用非保留(已分配)实例变量,如下所示:

__cpt_weak CPTAnnotationHostLayer *annotationHostLayer;

with the matching property definition of 具有匹配的属性定义

@property (nonatomic, readwrite, cpt_weak_property) __cpt_weak CPTAnnotationHostLayer *annotationHostLayer;

For targets of iOS 5.0 and Lion using the LLVM Compiler 3.0, this makes these properties use the safer __weak qualifier. 对于使用LLVM编译器3.0的iOS 5.0和Lion的目标,这使得这些属性使用更安全的__weak限定符。 For iOS 4.0 and Snow Leopard under LLVM Compiler 3.0, this turns to __unsafe_unretained . 对于LLVM编译器3.0下的iOS 4.0和Snow Leopard,这将变为__unsafe_unretained Finally, for any other compiler, the qualifier is blank and the property is set to assign . 最后,对于任何其他编译器,限定符为空,并且属性设置为assign

retain can be used for properties without much of a problem across all compilers. retain可以用于所有编译器中没有太大问题的属性。

You are using wrong complier. 你使用错误的编译器。 LLVM GCC does not supports Strong and other ARC keywords. LLVM GCC不支持强关键字和其他ARC关键字。 Please set your complier as LLVM compiler 4.2 请将您的编译器设置为LLVM编译器4.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM