Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does @property copy in combination with readonly make sense?

If I understand this correctly, copy enforces the setter to create a copy of the object passed in. However, if I use it together with readonly, there won't be a setter. So is my assumption correct, that combining @property (copy, readonly) doesn't make any sense or am I missing something?

like image 520
znq Avatar asked Feb 22 '12 15:02

znq


2 Answers

It does make sense. For instance, if you want to access a property's setter in your implementation only:

@interface MyClass : NSObject @property (nonatomic, copy, readonly) NSData *data;  - (id)initWithData:(NSData *)data;  @end 

and in the class continuation in the .m file:

@interface MyClass () @property (nonatomic, copy, readwrite) NSData *data; @end 

Note that the copy, readonly declaration in the public header is required in this case!

like image 150
Era Avatar answered Sep 20 '22 14:09

Era


According to Apple's documentation (which I've linked here for you):

copy
Specifies that a copy of the object should be used for assignment.

The previous value is sent a release message.

The copy is made by invoking the copy method. This attribute is valid only for object types, which must implement the NSCopying protocol.

So yes, you're correct... readonly creates a getter method and copy would be effectively ignored, since there's no setter method that does assignment.

like image 44
Michael Dautermann Avatar answered Sep 19 '22 14:09

Michael Dautermann