Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an Objective-C readonly property need to specify strong or copy?

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;
like image 385
Boon Avatar asked Feb 23 '12 00:02

Boon


People also ask

What is readonly in Objective C?

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.

What is strong property in Objective C?

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.

Can readonly property be set in constructor?

You can initialize a ReadOnly property in the constructor or during object construction, but not after the object is constructed.

What is the strong attribute of property?

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.


1 Answers

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.

like image 158
yuji Avatar answered Oct 05 '22 23:10

yuji