简体   繁体   中英

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?

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.

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.

If you wanna concatenate two strings use NSMutablestring and method appendstring instead of NSString.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html

You need to use NSMutableString.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM