In my interface (.h) file, I have
@property(readonly) NSString *foo;
and in my implementation (.m) file, I have
@synthesize foo;
With ARC turned on, the compiler gives me this error: Automatic Reference Counting Issue: ARC forbids synthesizing a property of an Objective-C object with unspecified ownership or storage attribute.
The error goes away if I add a strong
, weak
, or copy
to the property. Why is this? Why would there be any differences between these things for a read-only property, what are those differences, and why does the programmer have to worry about them? Why can’t the compiler intelligently deduce a default setting for a read-only property?
Another question while I’m at it: strong
, weak
, or copy
are the only things that make sense in ARC, right? I shouldn’t be using retain
and assign
anymore, should I?
Best Answer
You've declared a
@property
that doesn't have a backing ivar. Thus, when the compiler sees@synthesize
, it tries to synthesize a backing ivar for you. But you haven't specified what kind of ivar you want. Should it be__strong
?__weak
?__unsafe_unretained
? Originally, the default storage attribute for properties wasassign
, which is the same as__unsafe_unretained
. Under ARC, though, that's almost always the wrong choice. So rather than synthesizing an unsafe ivar, they require you to specify what kind of ivar you want.