Tarun Kumar

Total Post:107

Posted by  Tarun Kumar
 690  View(s)
Rate this:
  1. Tarun Kumar


    Define Mutability

    If we create an Objective-C string literal @"mutable_example", then actually we create constant string. The compiler will put this string in the constants section of the binary, if we will try to modify it, it will cause a segmentation fault.

    If we create a new string and copying string can make a program very slow, however this is one of the reasons Java code has a reputation for being slow; Java's String class is immutable, and since it is declared final you can't use Cocoa's solution to the problem, a mutable subclass.

    The NSString object is an immutable string, it has a subclass called "NSMutableString", because the mutable version is a subclass, it can be used anywhere that the immutable version can. It implements all of the same methods.

    The distinction between mutable and immutable objects is most apparent in the implementation of the -copy method. When you send a -copy message to an immutable object, you often get the same object back, because you cannot modify either "copy" they can never become different from each other.



Enter your email address here always to be updated. We promise not to spam!