Reputation: 22559
When I declare a property without a storage specification + instance variable, everything works happily:
@interface Foo : NSObject {
NSSomething* mySomething;
}
@property (readonly) NSSomething* mySomething;
Yet, when I delete the instance variable declaration, I get a compilation error stating that I must specify the storage type:
@property (readonly, strong) NSSomething* mySomething;
This made me wonder should I just declare strong, but it should be readonly anyway.. What is happening here? Which is approach should I use for which case?
EDIT (For those unable to replicate the problem):
Upvotes: 1
Views: 693
Reputation: 22493
You have it right. If you want it readonly, leave the readonly in there for sure.
If you want the ivar to be synthesised for you, though, the compiler needs to know whether to make it strong, weak, or unsafe untrained. The default is assign which translates to unsafe unretained (which probably isn't what you want). Others are reporting that the compiler handles the default properly without warning, but since you're having trouble, you need the retain attributes in the property declaration.
Alternatively, you can declare the ivar yourself as you originally did.
Upvotes: 3
Reputation: 855
You should specify either strong or weak, depending on whether you are willing that yourSomething will vanish when someone else stops referencing to it. If the property is based on an instance variable the property is automatically strong.
Also, independently of above decision, you should set the property to read only if you don't want it to have a setter (so nobody will be able to set yourObject.yourSomthing=something). This means that (unless you privately redeclare the property as readwrite in the .m file) there is no way to directly set yourSomething to anything. This can be useful if the getter doesn't rely on a variable but instead computes the returned value by some other means.
Upvotes: 1