views:

77

answers:

3
NSMutableString *str, *str1;

//allocation here

i am using [str appendString:str1] is not working. [str appendFormat:str1] is not working.

So, how to append a NSMutableString with another NSMutableString.

@str is an empty string initialize to nil. str1 has some value. [str appendString str1] returns null

A: 
NSMutableString *mystring = [NSMutableString stringWithFormat:@"pretty"];
    NSMutableString *appending = [NSMutableString stringWithFormat:@"face"];
    [mystring appendString:appending];

works for me...

are you sure the variables have been allocated (correctly?)? No misspellings?

Thomas Clayson
+2  A: 

Seems like your're sending a message to nil. nil is NOT an object, it's just nothing. Sending a message to nothing just return nothing. In order to append these strings, you need to initialize to an empty string. Like so:

NSMutableString *str = [NSMutableString string];

Then your code will work. Like so:

[str appendString:str1];
Max Seelemann
A: 

if str == nil, no call are performed because there is no object allocated to receive the message but no exception are raised (messages sent to nil return nil by design in Objective-C).

NSMutableString *str, *str1;

str = [NSMutableString stringWithString:@"Hello "];
str1 = [NSMutableString stringWithString:@"World"]; 

NSMutableString *sayit = [str appendString:str1];
VdesmedT
Then how to set my My NSMutableString to null. Because some point of my program i want to set str1 to empty.
It's maybe time to explain more precisely what you exactly want to do... Btw, Empty and nil are different things
VdesmedT
[NSMutableString string] creates an empty mutable string I.e. ""
VdesmedT