简体   繁体   中英

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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