Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use delegates with Automatic Reference Counting

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.

like image 423
Mike S Avatar asked Jun 30 '11 01:06

Mike S


People also ask

What is automatic reference counting in Swift explain how it works?

Swift uses Automatic Reference Counting (ARC) to track and manage your app's memory usage. In most cases, this means that memory management “just works” in Swift, and you don't need to think about memory management yourself.

What is OBJC ARC?

Automatic Reference Counting (ARC) is a memory management option for Objective-C provided by the Clang compiler. When compiling Objective-C code with ARC enabled, the compiler will effectively retain, release, or autorelease where appropriate to ensure the object's lifetime extends through, at least, its last use.

Why is automatic reference counting a type of garbage collection mechanism?

Automatic Reference counting or ARC, is a form of garbage collection in which objects are deallocated once there are no more references to them, i.e. no other variable refers to the object in particular.


1 Answers

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

like image 102
tc. Avatar answered Sep 24 '22 11:09

tc.