Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
Difference between NSString and NSMuttableString       Share
2010-07-15 |  RatheeshTR  | Viewed: 1218  |    2

Difference between NSString and NSMuttableString:


//  Setup  two  variables  to  point  to  the  same  string
NSString * str1 = @"Hello World";

NSString * str2 = str1;


//  "Replace"  the  second  string
str2 = @"Hello ikilimnik";


//  And  list  their  current  values
NSLog(@"str1 = %@, str2 = %@", str1, str2);


Mutable strings


//  Setup  two  variables  to  point  to  the  same  string
NSMutableString * str1 = [NSMutableString stringWithString:@"Hello World"];

NSMutableString * str2 = str1;


//  "Replace"  the  second  string
[str2 setString:@"Hello ikilimnik"];


//  And  list  their  current  values
NSLog(@"str1 = %@, str2 = %@", str1, str2);


Notice when you use the immutable NSString class that the only way to "replace" a string is to create a new string and update your variable "str2" to po
int to it. 
This however doesn't affect what "str1" is pointing to, so it will still reference the original string.


In the NSMutableString example, we don't create a second string, but instead alter (mutate) the contents of 

the existing "Hello World" string. Since both variables continue to po
int to the same string object, they will both report the new value in the call to NSLog.

It's important to differentiate between a pointer variable and the actual object it points to. 

A NSString object is immutable, but that doesn't stop you from changing the value of a variable which points to a string.


The data type "NSString *" is a pointer to a NSString object, not the object itself. 

If you set a break po
int at either of the NSLog statements within the XCode debugger, you may like to check the raw value of each variable to clarify this.


Reference:


http:
//www.cocoadev.com/index.pl?NSString

http:
//stackoverflow.com/questions/1805442/nsstring-immutable-allows-to-change-its-values


Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
Iphone Cocoa Programming tutorials
Iphone BubbleLevel Tutorials
Iphone UIImage Class Reference ( objective C )
Objective -C Date Formatter Examples -Iphone stringFromDate
iPhone Coding: Slider tutorial
Frequently used Iphone SDK API list

Related References
objective-c tutorial: nsarray -- arraywithcontentsofurl
iphone sdk: get contacts with the iphone sdk sample program
iphone sdk - local notification sample code (os 4.0 only)
resize uiimage sample tutorial

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us