In C#, we can declare an instance variable as readonly
to specify that it may only be the subject of an assignment during declaration or in the constructor of the class to which it belongs:
private readonly int _myInt;
In Objective-C, I understand that a property can be declared as readonly thus:
@property (readonly) int myInt;
However, this is equivalent to a C# property with a private set
accessor and does not protect the corresponding instance variable from being reassigned "privately" after initialization.
Is there a way to mimic the behaviour of C#'s readonly
modifier in Objective-C, such that an instance variable can only be assigned to in the initializer of its containing class (note that - because I would like to be able to assign to the variable during the class initializer, the const
modifier does not suit my needs)?
If this is not possible, is there a conceptual reason why this behaviour would not be appropriate in Objective-C?