ARC and weak IBOutlet properties
Asked Answered
D

9

14

I just updated a project to make use of ARC with the Xcode 4.2 built-in conversion tool. Unfortunately there is a weird bug appearing that I don't understand and didn't find anything about. I have a document class with a property declaration:

@property (weak) IBOutlet WebView *webView;

In the implementation file I have a @synthesize statement:

@synthesize webView=_webView;

When I try to compile it, it fails and tells me:

error: @synthesize of 'weak' property is only allowed in ARC or GC mode

Of course the project is tagged to compile with ARC. I would highly appreciate any help to understand what I'm doing wrong and why.

Edit: Was late yesterday. So here is a more complete compile-log:

[...] -fobjc-arc -Wno-trigraphs -fpascal-strings -O0 -Wmissing-prototypes -Wreturn-type -Wparentheses -Wswitch -Wno-unused-parameter -Wunused-variable -Wunused-value -Wshorten-64-to-32 -DDEBUG=1 -isysroot /Developer/SDKs/MacOSX10.7.sdk -fasm-blocks -mmacosx-version-min=10.7 -gdwarf-2 -Wno-sign-conversion "-DIBOutlet=attribute((iboutlet))" "-DIBOutletCollection(ClassName)=attribute((iboutletcollection(ClassName)))" "-DIBAction=void)attribute((ibaction)" -iquote [...]/Build/Intermediates/[...].build/Debug/[...].build/[...]-generated-files.hmap -I[...]/Build/Intermediates/[...].build/Debug/[...].build/[...]-own-target-headers.hmap -I[...]/Build/Intermediates/[...].build/Debug/[...].build/[...]-all-target-headers.hmap -iquote [...]/Build/Intermediates/[...].build/Debug/[...].build/[...]-project-headers.hmap -I[...]/Build/Products/Debug/include -I[...]/Build/Intermediates/[...].build/Debug/[...].build/DerivedSources/x86_64 -I[...]/Build/Intermediates/[...].build/Debug/[...].build/DerivedSources -F[...]/Build/Products/Debug -fno-objc-arc [...]

It seems the compiler-settings for ARC are turned on in the beginning and turned off again later in the arguments list. To be honest: I don't know where to remove such weird settings and how it came to this. The only solution I would come up with now, would be to start the complete project over from a blank and new one and import all the class files from scratch.

If someone knows an easier way, I would appreciate very much.

PS: I do have all build-settings concerning the ARC set to YES.

Dismount answered 15/10, 2011 at 19:49 Comment(8)
What happens when you remove the (weak)?Pinkard
Double-check your build settings. Click on your project in the file navigator, select the "Build Settings" tab and search for "Automatic Reference Counting." Then, ensure the field for "Objective C++ Automatic Reference Counting" is set to Yes.Hiro
@Philibbo: If I remove the weak and replace it with an assign, everything compiles. Is it right, when I assume that this file seems to be excluded from the ARC somehow?Dismount
@Stephen: Objective C++ Automatic Reference Counting is of course set to YES. For me it appears as if this class seems to be excluded from the ARC somehow. Can this be the case?Dismount
Go to the build log and find the build output for the file you're compiling. You should see '-fobjc-arc' in the compiler flags. If you don't, it means something is off in your build settings.Hiro
@Stephen: Found ".../Build/Products/Debug -fno-objc-arc" for every single file that gets compiled. Only the document class fails ending in the following: '...Document.m:31:13: error: [at]synthesize of 'weak' property is only allowed in ARC or GC mode [3] [at]synthesize webView=_webView; ^ ...Document.h:16:36: note: property declared here [3] [at]property (weak) IBOutlet WebView *webView; ^ 1 error generated.'Dismount
Those build logs show you're not compiling in ARC mode. -fno-objc-arc is opting out of ARC. Make sure ARC really is turned on for the project/target you're building...Hiro
@Stephen: Indeed I do, please see my last edit to the question. The project seems to have both compiler flags set: -fobjc-arc and -fno-objc-arc. Does someone know how and where to fix such a setting?Dismount
D
1

I took some more time today and did a new conversion from a previous commit of the project. This time everything went well. I presume this error emerged out of me trying to do the conversion in 2 separate steps. The project is split into 2 components. I wanted to convert them separately. Thats why I didn't select all the files during the first conversion run. To my amazement the first conversion ran very painlessly and in shorter time than I expected. So I decided to convert the second component of the project too. What I ended up with, was the project state described as the problem above: conflictive build-settings.

A good advice might be not to try to convert a project in steps, but take a couple of more time and do it at once. I must assume: the conversion-function of Xcode might not be meant for such a procedure.

Dismount answered 16/10, 2011 at 17:59 Comment(1)
This was a no brainer really. If you convert in steps, you will need to set the -fno-objc-arc flag on the files you excluded from your initial conversion step. The compiler complained b/c the files that were not yet ARC compliant were not flagged as such. This post covers it pretty fully >> longweekendmobile.com/2011/09/07/…Dodecahedron
A
11

In only ARC we have to use weak property and in without ARC we have to use unsafe_unretained it is same means for ARC we have to use

@property (weak) IBOutlet WebView *webView;

and for without ARC

@property(unsafe_unretained) IBoulet WebView *webView; 
Alltime answered 28/12, 2012 at 6:21 Comment(0)
I
6

FYI: For anyone else plagued by the "@synthesize of 'weak' property is only allowed in ARC or GC mode" error:

If you've tried all the solutions suggested in this thread and it still won't build, try closing the Xcode project (File > Close Project) then re-open the project and try building again.

Indaba answered 12/11, 2011 at 1:32 Comment(0)
M
2

For anyone else running into this, if you converted your project into parts to ARC, verify you don't have the -no-obj-arc flag on the files it's complaining about.

Mullin answered 1/6, 2012 at 21:19 Comment(0)
D
1

I took some more time today and did a new conversion from a previous commit of the project. This time everything went well. I presume this error emerged out of me trying to do the conversion in 2 separate steps. The project is split into 2 components. I wanted to convert them separately. Thats why I didn't select all the files during the first conversion run. To my amazement the first conversion ran very painlessly and in shorter time than I expected. So I decided to convert the second component of the project too. What I ended up with, was the project state described as the problem above: conflictive build-settings.

A good advice might be not to try to convert a project in steps, but take a couple of more time and do it at once. I must assume: the conversion-function of Xcode might not be meant for such a procedure.

Dismount answered 16/10, 2011 at 17:59 Comment(1)
This was a no brainer really. If you convert in steps, you will need to set the -fno-objc-arc flag on the files you excluded from your initial conversion step. The compiler complained b/c the files that were not yet ARC compliant were not flagged as such. This post covers it pretty fully >> longweekendmobile.com/2011/09/07/…Dodecahedron
P
1

Try marking explicitly with -fobjc-arc as compiler flags in the Build Phases

Presidency answered 16/10, 2012 at 3:27 Comment(0)
M
0

In case anyone else stumbles over this:

I just fixed this issue by setting "Objective-C Garbage Collection" to "Unsupported" in the Build Settings. This was set to YES for some reason and that caused the ARC build not to work.

Marylyn answered 20/1, 2012 at 15:46 Comment(0)
P
0

Ok, just in case anyone else has the same issue as me, I was adding UI elements as properties below the closing "}" of my @interface, instead of inside. Once I deleted the @property and @synthesize entries in the .h/.m files and re-linked the UI elements within the @interface{ } section, it worked.

Pentomic answered 10/7, 2012 at 20:54 Comment(0)
S
0

In my case I found that changing the Build Setting "Compiler for C/C++/Objective-C" in the category 'Build Options' to the default ("Apple LLVM compiler 3.0") resolved this issue.

It had been set to "LLVM GCC 4.2", and further down, under category '... complier 3.0 - Language', the option to select ARC ("Objective-C Automatic Reference Counting") wasn't even visible.

This was particularly bizarre because I have another project previously converted to ARC, and while the selected compiler was also "LLVM GCC 4.2", nonetheless the 'Language' category was still titled 'Apple LLVM compiler 3.0', and the option to select ARC was visible.

Same compiler selected, yet different options offered for that compiler. Weird.

Sanctity answered 24/1, 2013 at 0:9 Comment(0)
D
0

I also came across the same problem. The mistake I committed was that in Build Phases-> Compile Sources I set the flag of a non ARC file as "-fno-objc-arc". I wanted it added to a non ARC file. I removed it and it started working correctly.

Defoliant answered 4/6, 2013 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.