What is the difference between stringByAppendingString
and appendString
? If NSString
is not mutable then how could it append string?
Difference between stringByAppendingString and appendString in ios
1.9k views Asked by abhiDagwar At
2
There are 2 answers
0
On
The documentation for stringByAppendingString
states that it
Returns a new string made by appending a given string to the receiver
so it doesn't modify the existing string - it can't, because as you noted, NSString is immutable
appendString
, on the other hand, is a mutating method of NSMutableString
which modifies the receiving NSMutableString
.
appendString:
is fromNSMutableString
,stringByAppendingString:
is fromNSString
.The first one mutates the existing
NSMutableString
.The second one returns a new
NSString
which is a concatenation of the receiver and the parameter.The reason for that is that the regular
NSString
is immutable and you cannot directly append something to it. The NSMutableString however as the name suggests is mutable and therefore can be modified.