Curnelious
Curnelious

Reputation: 1

StringByAppendingString function does not work?

 NSMutableString *chars=[ [NSMutableString alloc] initWithString:@""]   ;
    NSString *temp=[[self convertDecimalToChar:(digitizer%10)] copy]; //temp is good
    [chars stringByAppendingString:temp]; //chars is empty

Any idea whats wrong here ? Thank you.

Upvotes: 1

Views: 933

Answers (3)

trojanfoe
trojanfoe

Reputation: 122458

The stringByAppendingString method is on the non-mutable NSString class, where non-mutable means you cannot modify it.

Therefore, as with most other NSString methods, it returns a new NSString object, in this case containing the original string plus whatever you passed in the parameter.

However given you are actually manipulating an NSMutableString object, which is mutable, you probably want the appendString: method instead:

[chars appendString:temp];

Upvotes: 1

Antonio MG
Antonio MG

Reputation: 20410

This line:

[chars stringByAppendingString:temp];

Is supossed to return a string combining both strings.

- (NSString *)stringByAppendingString:(NSString *)aString

If you want to just append a string to your string, do this:

[chars appendString:temp];

Find the documentation here:

NSmutableString

Upvotes: 2

talnicolas
talnicolas

Reputation: 14051

If you take a look at the documentation of this method, you need to do:

chars = [chars stringByAppendingString:temp];

It is returning a new NSString combining the two NSString, not actually changing the receiver.

Upvotes: 0

Related Questions