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?
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.
Think about using an NSMutableString - you can use the appendString:
method, as in:
NSMutableString *str = [@"hello" mutableCopy];
[str appendString:@" world!"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With