31

I used to declare all delegate properties as

@property (assign) id<FooDelegate> delegate;

I was under the impression that all assign properties should now be weak pointers, is this correct? If I try to declare as:

@property (weak) id<FooDelegate> delegate;

I get an error while trying to @synthesize (autogenerated weak properties are not supported).

What's the best practice in this case?

Philipp Schlösser
  • 5,179
  • 2
  • 38
  • 52
cfischer
  • 24,452
  • 37
  • 131
  • 214
  • This is a good question and the answers are not very conclusive. I would have thought to use weak but the Apple frameworks use assign (see UIPageViewController which is only available on iOS5+). – Paul de Lange May 15 '13 at 06:47

2 Answers2

23

Xcode 4 Refactor > Convert to Objective-C ARC transforms:

@interface XYZ : NSObject
{
    id delegate;
}
@property (assign) id delegate;
...
@synthesize delegate;

into:

@interface XYZ : NSObject
{
    id __unsafe_unretained delegate;
}
@property (unsafe_unretained) id delegate;
...
@synthesize delegate;

If I remember correctly it is also mentioned in WWDC 2011 video about ARC.

djromero
  • 19,551
  • 4
  • 71
  • 68
22

Use __unsafe_unretained instead weak for ARC projects targeting iOS 4 and 5. The only difference is that weak nils the pointer when deallocated, and it's only supported in iOS 5.

Your other question is answered in Why are Objective-C delegates usually given the property assign instead of retain?.

Community
  • 1
  • 1
Jano
  • 62,815
  • 21
  • 164
  • 192