11
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

user426795
  • 11,073
  • 11
  • 35
  • 35
  • 2
    In order for us to help you, we would need to know what "not working" means in your case, because our crystal ball is currently not working :) According to Apples documentation, both methods should do what you are trying to do ( http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableString/appendString: ) – fresskoma Aug 30 '10 at 11:45
  • What does "does not work" mean? Exceptions? – tobiasbayer Aug 30 '10 at 11:46
  • does not work means not able to append. – user426795 Aug 30 '10 at 11:59
  • After alloc method, does you call initWithXXX or init, to initialize the variable you allocated ? – AechoLiu Aug 30 '10 at 12:13
  • Give an example, please. What is the value of `str` before and after the call? – Max Seelemann Aug 30 '10 at 12:14
  • @Max, str is an empty string initialize to nil. str1 has some value. [str appendString str1] returns null – user426795 Aug 30 '10 at 12:22

4 Answers4

21

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
  • 9,344
  • 4
  • 34
  • 40
3

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
  • 9,037
  • 3
  • 34
  • 50
1

Swift version

Although in Swift it is not the general practice to use NSMutableString, it may at times be necessary. This is how you would do it:

var str: NSMutableString = NSMutableString()
var str1: NSMutableString = "some value"
str.appendString(str1 as String) // str = "some value"

Note that the usual way to append a string in Swift is like this:

var str = ""
let str1 = "some value"
str += str1

where str and str1 are inferred to be of type String.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
0
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
  • 29,657
  • 26
  • 147
  • 224