If I have a read-only string property, is it necessary to specify strong
(or retain
) or copy
in the declaration? If I don't specify, is one of them assumed?
It seems to me the ownership attribute is only useful when you have a setter.
@property (nonatomic, readonly) NSString *name;
The readonly means simply that no setter method was synthesized, and therefore using the dot notation to set a value fails with a compiler error. The dot notation fails because the compiler stops you from calling a method (the setter) that does not exist.
strong (default) Strong just means you have a reference to an object and you will keep that object alive. As long as you hold that reference to the object in that property, that object will not be deallocated and released back into memory.
You can initialize a ReadOnly property in the constructor or during object construction, but not after the object is constructed.
strong / retain : Declaring strong means that you want to “own” the object you are referencing. Any data that you assign to this property will not be destroyed as long as you or any other object points to it with a strong reference.
That is mostly correct. For a readonly
property, strong
, retain
, weak
, and assign
have no effect. But if you also declare the property elsewhere as readwrite
(most frequently in an anonymous category in the .m
), then the other modifiers need to match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With