简体   繁体   中英

Simple string concatenation in Objective C

I have an NSString named 'you' with value "This is a you string!".

I want to concat "123" in 'you', how can I do it?

I am using this code and it is giving an error.

you=[you stringByAppendingString:@"123"];

This code here is working for me

NSString *s = @"avant";
s = [s stringByAppendingString:@" - après"];
NSLog(@"%@", s);

2012-01-13 11:48:59.442 tabbar[604:207] avant - après

So my guess is that your you is a bad pointer that is not nil and not the NSString you think it have.

Have you try an NSLog on that value before the call?

你也可以试试这个:

you = [NSString stringWithFormat:@"%@%@", you, @"123"];

Code :

NSString *you;
you = @"This is you String!";
NSLog(@"you : %@ ",you);

you = [you stringByAppendingString:@"123"];
 NSLog(@"you : %@ ",you);

[you stringByAppendingFormat:@"%@%@",you,@"123"];
NSLog(@"you : %@ ",you);

Result in Console :

[233:907] you : This is you String!

[233:907] you : This is you String!123

[233:907] you : This is you String!123

Please try this

NSString *version = @"14.5.1";

NSString *build = @"1.0";


self.versionLabel.text = [NSString stringWithFormat:@"%@%@%@%@%@" , @"V : " ,version,@" ( ",build, @" )" ];

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