Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective-c concatenate NSString

I have problems to concatenate NSString.

Each time I pushed a button I want that something ("aux") is added to my string ("myString"). so:

NSString *aux = [NSString stringWithFormat: @"%d", buttonIndex];

myString=[NSString stringWithFormat:@"%@/%@",posTargetaText,aux];

aux = nil;

The first time i pushed the button it works good but the second it doesn't work.

Some help please?

like image 661
ValentiGoClimb Avatar asked Jun 29 '11 07:06

ValentiGoClimb


People also ask

How to append string to NSString in Objective C?

The format specifier “%@” is used to insert string values. The example below uses a combination of whitespace and %@ format specifiers to append two strings to string1: NSString * string3 = [string1 stringByAppendingFormat:@" %@ %@", string2, @"A third string. "];

What is NSString?

A static, plain-text Unicode string object which you use when you need reference semantics or other Foundation-specific behavior.


2 Answers

So you can certainly use stringWithFormat, but why don't you use stringByAppendingString instead, since that's exactly what you want to do?

NSString *newString = [firstString stringByAppendingString:secondString];

You really don't need to use a mutable string unless you have a compelling reason to.

like image 183
lxt Avatar answered Sep 18 '22 08:09

lxt


Not sure what exactly you want to do. But as per your code aux will have new buttonIndex value each time and You will have always new mystring when ever you tap button.

If you want to append string always in myString that you need to do like this.

myString=[NSString stringWithFormat:@"%@%@/%@",myString,posTargetaText,aux];

You suppose to add previous value of myString as well in new myString string ?

Not sure this is what you want or something different. Please explain in detail if this is not.

like image 41
Deeps Avatar answered Sep 20 '22 08:09

Deeps