what is actual difference between NSString and NSMutable String in objective C with the help of example? -
this question has answer here:
what actual difference between nsstring , nsmutable string in objective c? have searched lot not getting answer..i understand nsstring immutable object , nsmutablestring mutable object want understand difference between them of example..please me..
now question has solved..
immutable string.....
nsstring *str1 = @"hello testing"; nsstring *str2 = str1; replace second string
str2 = @"hello hehehe"; and list current values
nslog(@"str1 = %@, str2 = %@", str1, str2); //logs below //str1 = hello testing, str2 = hello hehehe mutable strings
setup 2 variables point same string
nsmutablestring * str1 = [nsmutablestring stringwithstring:@"hello testing"]; nsmutablestring * str2 = str1; replace second string
[str2 setstring:@"hello ikilimnik"]; // , list current values
nslog(@"str1 = %@, str2 = %@", str1, str2); //logs below //str1 = hello ikilimnik, str2 = hello ikilimnik notice when use immutable string class way replace string create new string , update variable str2 point it.
this doesn't affect str1 pointing to, still reference original string.
in nsmutablestring example, don't create second string, instead alter contents of existing hello testing string. since both variables continue point same string object, both report new value in call nslog.
it important differentiate between pointer variable , actual object points to. nsstring object immutable, doesn't stop changing value of variable points string.
Comments
Post a Comment