Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"]; 
like image 993
Umair Khan Jadoon Avatar asked Jan 13 '12 16:01

Umair Khan Jadoon


People also ask

How do you concatenate strings in Objective C?

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. "]; The objects are inserted in the order that they are passed into the method.

What is NSString?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.


2 Answers

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?

like image 125
Vincent Bernier Avatar answered Oct 14 '22 18:10

Vincent Bernier


You can try this also:

you = [NSString stringWithFormat:@"%@%@", you, @"123"]; 
like image 42
Spidey Avatar answered Oct 14 '22 16:10

Spidey