Getting “Expected a property attribute before '

2020-02-05 08:15发布

I have a project that contains some files that are using ARC, and some are not. The ones that are not have the compiler flag that disables ARC. That works fine.

I also want to make sure my library compiles for LLVM-GCC and also LLVM Compiler.

I have a property like this:

@property (strong, nonatomic) NSString *foo;

However, when I compile in LLVM-GCC, I get:

"Expected a property attribute before 'strong'"

If I change strong to retain, it compiles fine. Strong also works fine in LLVM Compiler.

What am I missing? Isn't strong synonymous with retain?

3条回答
放我归山
2楼-- · 2020-02-05 08:34

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.

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. 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.

To do this, I drew from Ryan Petrich's ZWRCompatibility, which he describes in his answer here, and assembled the following defines:

#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. For iOS 4.0 and Snow Leopard under LLVM Compiler 3.0, this turns to __unsafe_unretained. Finally, for any other compiler, the qualifier is blank and the property is set to assign.

retain can be used for properties without much of a problem across all compilers.

查看更多
家丑人穷心不美
3楼-- · 2020-02-05 08:47

You are using wrong complier. LLVM GCC does not supports Strong and other ARC keywords. Please set your complier as LLVM compiler 4.2

查看更多
爷、活的狠高调
4楼-- · 2020-02-05 08:49

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.

查看更多
登录 后发表回答