Enforcing copy semantics for users of my Objective-C class -


i have objective-c class that's intended copy semantics.

 @property (copy) viewstate* viewstate; 

it’s not immutable, hangs on viewstate instance needs own distinct copy. in fact, if other class mistakenly tries

 @property (strong) viewstate* viewstate; 

we'll crash.

how enforce or encourage client classes use correct semantics?

in c++, example, prohibit assignment

 private:  cpviewstate* operator=(const cpviewstate*) const;  // no implementation 

but can't in objective-c. opposite case, want prohibit copying, can log error or throw exception copywithzone:.

how can either require copy semantics or, @ least, make clear future developers want use copy semantics?

copy semantics cannot enforced within class itself, correctly explained.

depending on , how viewstate objects created, , means conceptually, there different things work.

i not sure viewstate means in project, if makes sense supply singleton/factory viewstatefactory class, have few methods creating and/or copying viewstate objects clients use. viewstatefactory ensure new copies supplied every time client requests viewstate object.

note adds boilerplate clients of class. doesn't prevent people going behind viewstatefactory , directly allocating and/or referencing viewstate objects. make more clear intentions , how want other people use class.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -