iphone - What is the logic behind having a mutable and immutable versions of classes like NSArray, NSDictionary etc in Objective C? -


why common collection classes in objective c nsstring, nsarray, nsdictionary etc have mutable immutable version. logic behind defining them separately? performance, memory management or else?

the immutable versions of classes exist because immutable object is, in , of itself, unique identifier particular state. i.e. if have nsarray of 100 nsstring instances, nsarray instance can treated idempotent 1 of strings.

as well, immutability means change cannot happen after state has been vended. example, nsview's subviews method returns immutable array, ensuring caller isn't going play games contents (nor expect able to). internally, nsview could choose return [likely] nsmutablearray contains subviews (since internally mutable) , typecast nsarray means caller can't manipulate contents without evil cast or bad compiler warning. (this may or may not real implementation, btw -- pattern used elsewhere).

immutability means enumeration and/or traversal can done without risk of state change in middle. similarly, many immutable classes explicitly thread safe; number of threads can simultaneously read immutable state, without need lock.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -