I read several other topics about it, but still I got lost.
I want to create 2 types of objects, one immutable only with read-only properties and one modified with only readwrite properties.
Lets call them EXCar and EXMutableCar.
EXCar is a subclass of NSObject, and EXMutableCar is a subclass of EXCar.
ExCar will have in its interface
@property (nonatomic, strong, readonly) NSString *name;
EXMutableCar will have in its interface
@property (nonatomic, strong) NSString *name;
So, I “open” the EXCar properties when I use it with a subclass of EXMutableCar. And then it is volatile. The problem is the proper copying between them.
I implemented mutableCopyWithZone in EXCar:
- (id)mutableCopyWithZone:(NSZone *)zone { EXMutableCar *mutableCopy = [[EXMutableCar allocWithZone:zone] init]; mutableCopy.name = _name; return mutableCopy; }
First question, is this a good way to do this? (I want to get a copy of the swallow)
The problem is copyWithZone. Since the EXCar properties are readonly, I cannot create a new EXCar instance in EXCar or EXMutableCar and populate its properties as follows:
- (id)copyWithZone:(NSZone *)zone { EXCar *copy = [[EXCar allocWithZone:zone] init]; copy.name = _name;
And I really don't want to do an “init” method with 15 properties to go through (of course, EXCar is an example, real classes are full of many properties). And usually they are initiated from a JSON message from the server, so they don’t need a complicated init method.
Second question: how to make copyWithZone that keeps my class unchanged?
Thank you for your help:)