Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Read-only properties and inheritance

I'm making a card game app where you can play two different card games.

I have a base class, which represents a basic card game. Then I have two subclasses of it which represent each game.

In my base class I declared some properties as readonly (I want only the game to modify the score, for example), but my subclasses cannot modify the score either, and as they need to implement different score mechanisms, they need to modify it.

(I declared the properties as readonly in the public API of my base class but as readwrite in my private API.)

So I deleted the readonly part of the property and deleted the overwrite in my .m file.

But I have another property in my base class which is also readonly. It is a Mutable Array and I remove all of its objects in a subclass.

Why is this possible? Is it the fact that it is a Mutable Array? (The other properties are NSIntegers) Does removing an object not count as a write operation?

If I try to change the other properties as readonly, Xcode complains about a semantics error.

Thanks for your help in advance!

3 Answers

Readonly don't let you to modify the variable's pointer as no setter method is created, but this doen's mean you can't modify the content of the variable in the case of NSMutableArray.

If you don't want anybody handle with your array just return a copy in the getter of the property used in basicClass.m and put it as NSArray readonly property in basicClass.h

Makes sense. Thanks!

Hi Andres,

NSInteger is not a class, it is a basic type like an int or long (in fact NSInteger is a typedef of int so is a primitive data). I would recommend you, for that purpose, to use NSNumber instead because is a class and can be readonly property.

Hope that helps

Good to know, anyway, that's not the problem because I have another readonly property which is an NSString and I cannot modify it in the subclasses as well.

I'll try to take a stab at this.

My guess is the use of "mutable" allowed you to readwrite that property. When a non-mutable object is created, any further references to it simply increases the retain count. However, for mutable objects, when a reference is placed to it, a copy is essentially created. This is I believe how memory is allocated between mutable and non-mutable objects.

This can easily be tested as well. Simply create a bunch of objects (NSStrings, NSArrays, NSDictionary) and make them all readonly. THEN, create another set of objects but this time mutable (NSMutableString, NSMutableArray, NSMutableDictionary) and make them all readonly. If you can readwrite the mutable ones but not the immutable ones then I guess that supports my argument.