For strong
, you can just use retain
. They're identical.
weak
is trickier, and while I know several ways that should work, I'm not certain the best way to handle it.
First, make sure you actually need it. If you're supporting iOS4, you can't have weak
anyway, so the issue is moot. My gut feeling is that I'd probably just avoid weak
and make all these problems go away. weak is nice, but it's not that big a deal to avoid in most cases.
That said, there are some approaches that will work. The best is probably to declare weak
accessors without properties in the header. Instead of this:
@property (nonatomic, readwrite, weak) id delegate;
Do this:
- (id)delegate;
- (void)setDelegate:(id)aDelegate;
Then you can still declare a weak
property inside your implementation file. The caller can still use dot notation for this, BTW.
It's possible that you'll get a compile error here, since setDelegate:
technically takes a __strong id
. If that's the case, just hand-implement setDelegate:
- (void)setDelegate:(id)aDelegate {
_delegate = aDelegate;
}
Haven't tested it, but that should work. You might also declare the ivar _delegate
to be __weak
in the @implementation
block rather than declaring it as a weak
property.
Like I said; I haven't tested any of this out. Please post your findings if it works.