Short answer
- Yes. No memory corruption, and no compile error if the iVar is accessible.
- Same as 1.
Long answer
Mutable and immutable copy is one of the core concepts found in the Apple's Foundation classes, such as NSString
or NSArray
. As stated in NSCopying
protocol, the class that has implement "immutable vs mutable" concept must return immutable object for copyWithZone:
.
However the immutability of the object is not enforced by the compiler or memory protection, but the class designer must design the class to make it return immutable object upon receiving copy
message.
In your case, the common way to implement such thing is to make MyClass
immutable, and subclass to MyMutableClass
and make it mutable. Your call site should look like:
MyClass *a = [x copy];
MyMutableClass *b = [x mutableCopy];
Note that implement mutable/immutable is sometimes quite tedious, so make sure you have enough reason to implement such feature.