Home > DeveloperSection > Interviews > Define Mutability

Posted on    November-02-2015 2:05 AM

 iPhone iPhone  Ios  Objective C 
 1 Answer(s)
  557  View(s)
Rate this:

Tarun Kumar

Total Post:107

Posted on    November-02-2015 3:05 AM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick