I have an Objective-C class that's intended for copy semantics.
@property (copy) ViewState* viewState;
Itβs not immutable, but everything that hangs on to a ViewState
instance needs its own distinct copy. In fact, if some other class mistakenly tries
@property (strong) ViewState* viewState;
we'll eventually crash.
How do we enforce or encourage client classes to use the correct semantics?
In C++, for example, we could prohibit assignment
private:
CPViewState* operator=(const CPViewState*) const; // no implementation
But we can't do that in Objective-C. For the opposite case, where we want to prohibit copying, we can log an error or throw an exception from copyWithZone:
.
How can I either require copy semantics or, at least, make it clear to future developers that they want to use copy semantics?