objective-c concatenate NSString

怎甘沉沦 提交于 2019-12-19 18:24:06

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


You need to use NSMutableString.



来源:https://stackoverflow.com/questions/6517113/objective-c-concatenate-nsstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!