Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending string to UILabel text?

Tags:

string

iphone

I'm starting to develop for the iPhone. I have a beginner-type question, I'm sure:

I have this, which works:

testLabel.text = [NSString stringWithFormat:@"%@ to %@", testLabel.text, newLabelText];

I wish I could use the "+=" operator, but I get a compile error (Invalid operands to binary +, have 'struct NSString *' and 'struct NSString *'):

testLabel.text += [NSString stringWithFormat:@"to %@", newLabelText];

Why can't I do this?

Also, how can I shorten my first snippet of code?

like image 207
Jeff Meatball Yang Avatar asked Dec 14 '22 02:12

Jeff Meatball Yang


2 Answers

You can't use the += operator because C and Objective-C do not allow operator overloading. You're trying to use += with two pointer types, which is not allowed -- if the left-hand side of a += expression has a pointer type, then the right-hand side must be of an integral type, and the result is pointer arithmetic, which is not what you want in this case.

like image 168
Adam Rosenfield Avatar answered Dec 27 '22 04:12

Adam Rosenfield


Think about using an NSMutableString - you can use the appendString: method, as in:

NSMutableString *str = [@"hello" mutableCopy];
[str appendString:@" world!"];
like image 42
Tim Avatar answered Dec 27 '22 03:12

Tim