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.
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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.