45

I've jumped on the ARC bandwagon. In the past I would have my delegate properties declared like this:

@property(assign) id<MyProtocol> delegate;

So I thought I would do this under ARC:

@property(weak) id<MyProtocol> delegate;

Not so. On the @synthesize statement in the .m I have a compile error:

*Semantic Issue: Existing ivar 'delegate' for __weak property 'delegate' must be __weak*

I HAVE declared it as weak though! Also how do I pass a class implementing a protocol to a weakly referenced property. Do I have to wrap it in one of those weird obj_unretained calls?

Any help on this would be very much appreciated.

Mike S
  • 4,092
  • 5
  • 35
  • 68
  • 2
    ARC can be discussed publicly. iOS 5 specific API cannot. – bbum Jun 30 '11 at 05:08
  • The implication is that iOS 5 supports GC Obj-C, which is presumably confidential unless Apple's announced it (I don't know if they have; I've been too busy to keep up). – tc. Jul 01 '11 at 02:50

1 Answers1

65

"ivar" means "instance variable", which you have not shown. I'm betting it looks something like this:

@interface Foo : NSObject {
    id delegate;
}

@property (weak) id delegate;

What the error is saying is that it must look like this:

@interface Foo : NSObject {
    __weak id delegate;
}

@property (weak) id delegate;

If the property claims to be weak, the ivar that the value ends up being stored in must be weak as well.

tc.
  • 33,468
  • 5
  • 78
  • 96
  • 43
    Or you could just remove the ivar altogether and let the `@synthesize` line handle that for you as well. :) – Dave DeLong Jun 30 '11 at 02:07
  • 3
    your assumption was correct. And DaveDelong - are you telling me that for 1.5 years I've been writing iOS with ivars in the header as well as the @property declaration and I don't need ivar when I synthesize?! this is amazing lol! Thankyou! – Mike S Jun 30 '11 at 04:32
  • 2
    @Mike don't feel to bad. The ability to do that sort of accreted across architectures and couplers in the last year+. It is now complete, though. – bbum Jun 30 '11 at 05:10
  • 1
    However, if an ivar is not declared it will not be visible in Xcode. (bug filed). – zaph Jun 30 '11 at 19:18
  • Adding _weak before id in my ivar declaration gives me the error: "Unknown type name "_weak". Kudos to anyone who respond to this so late in the the game (: – Dylan Reich Aug 11 '11 at 06:32
  • 4
    @Dylan: You need two underscores. – tc. Sep 08 '11 at 14:57
  • 1
    But the answer does not work for a 10.6 target build: "The current deployment target does not support automated weak references" Syntax torn between two OS revisions :( – ctpenrose Dec 09 '11 at 21:14
  • @ctpenrose: If you still need to support pre-10.7/5.0, you should be using something like `{ __unsafe_unretained id foo; } @property(assign) id foo;`. You could cook up some `#define`s and build different versions, but then you'd need to test both of them. Alternatively, there's a possibility that someone will add runtime support to 10.5/10.6, just as they did with "blocks" (whether the compiler will let you use it is another matter entirely). – tc. Dec 21 '11 at 14:12
  • I've found out since if anyway is interested that not declaring the ivars means you can't look at the values in when debugging. – Mike S May 16 '12 at 01:29
  • 1
    @MitchR: That's a bug in GCC/GDB (one doesn't generate debug symbols, perhaps?). I get around it by simply calling the getter method. – tc. Jun 11 '12 at 15:26
  • ( nonatomic , strong ) is also good options.. it works for me. – Solid Soft Jul 30 '13 at 05:28