Is it possible to assign values to a property of a readonly object?
Er, sort of. The language you used is slightly confusing. It isn't a readonly object; it's a readonly property. And it is possible to assign values to the object stored in a readonly property.
readonly
just means that you can't set that property -- there is no setModelProperty:
setter method.
It does not mean that the target of the object is immutable. It's not analogous to const
. All it means is that you can't set the property.
The reason you're seeing nil
is that any message sent to nil
returns nil
.
self.modelProperty.arrayA
Is the same as:
[[self modelProperty] arrayA]
If modelProperty
is nil
(which it is, from the code you've shown), this is the same as:
[nil arrayA]
Which returns nil
-- in the same way that [nil anyMethod]
returns nil
.
Final note: you have copy
set as the memory policy for the modelProperty
property, but the ModalA
class doesn't appear implement the NSCopying
protocol. Are you sure you don't want strong
?