objective c - stringByAppendingString on same object -


I have a question on NSString , so the question is:

< Pre> NSString str = @ "Hello"; Str = [str stringByAppendingString: @ "world"];

If we get NSLog to str we will get an output - HelloWorld

So my question is that if str is NSString class variable one can not be changed after its one constant (which has been defined) ) So how can we change it, (note that I have used the same NSString object str ).

You have not declared the string as static, but NSString It is irreversible that you are creating a new string and replacing str is an unstable type in many OBJ-C classes, so if you want to modify this string as an example, So you want something else:

  NSMutableString * str = [NSMutableString stringWithString: @ "Hello"]; [Stradded string: @ "world"];  

Note, @ "Hello" is a NSString , so by trying to start a NSMutableString using that syntax An error will occur


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -