Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using weak with readonly property?

Should one use

@property (nonatomic, weak, readonly)

or

@property (nonatomic, readonly)?

Weak has the advantage of nil-ing out the instance if it gets deallocated, but is readonly implying weak? Should one explicit declare a property as weak if it want the weak behaviour?

like image 938
Meda Avatar asked Mar 06 '13 18:03

Meda


2 Answers

If you want to keep a pointer to an object that you don't own but want it to be valid only as long as it exists, then you want to use a weak pointer because when it gets deallocated by the owner, your pointer will automatically get set to nil and won't be pointing to memory that it shouldn't be.


These both have differnect meaning, readonly doesn't make any differnce if it is weak or strong.

@property (nonatomic, weak, readonly)
@property (nonatomic, readonly)

You can also find some reference here.

like image 150
Anoop Vaidya Avatar answered Nov 11 '22 14:11

Anoop Vaidya


Weak or strong is by no means related to readonly or readwrite. None implies the other.

A strong relation takes ownership. A weak does not but it receives the service of being nullified upon deletion of the related object.

Readonly suppresses a setter (afaik). The property cannot be changed from outside its class. Readwrite (wich is the default if none is stated) allows changes to the property.

That's basically it. That are two settings which are not related to each other. They work in all thinkable comibnations.

like image 2
Hermann Klecker Avatar answered Nov 11 '22 15:11

Hermann Klecker