views:

641

answers:

5

Hi, 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?

+1  A: 

Try to use the solution posted here.

luvieere
+2  A: 

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

NSMutableString *str = [@"hello" mutableCopy];
[str appendString:@" world!"];
Tim
+1  A: 

That can't be done because ObjectiveC does not support it, ObjectiveC is a small layer over C.

testLabel.text = [testLabel.text stringByAppendingFormat:@" to %@", newLabelText];
zaph
+2  A: 

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.

Adam Rosenfield
+1  A: 

NSString are NOT mutable (they can't be changed), that's why you can't use +=.

NSMutableString can be changed. You might be able to use them here.

Your code is already pretty minimal. Objective-C is a expressive language so just get used to long, descriptive function and variable names.

ACBurk