将字符串附加到UILabel文本?

Jef*_*ang 4 string iphone

我开始为iPhone开发了.我有一个初学者类型的问题,我敢肯定:

我有这个,有效:

testLabel.text = [NSString stringWithFormat:@"%@ to %@", testLabel.text, newLabelText];
Run Code Online (Sandbox Code Playgroud)

我希望我可以使用"+ ="运算符,但是我得到一个编译错误(无效的操作数到二进制+,有'struct NSString*'和'struct NSString*'):

testLabel.text += [NSString stringWithFormat:@"to %@", newLabelText];
Run Code Online (Sandbox Code Playgroud)

为什么我不能这样做?

另外,如何缩短我的第一段代码?

Tim*_*Tim 5

考虑使用NSMutableString - 您可以使用该appendString:方法,如:

NSMutableString *str = [@"hello" mutableCopy];
[str appendString:@" world!"];
Run Code Online (Sandbox Code Playgroud)


Ada*_*eld 5

您不能使用+=运算符,因为C和Objective-C不允许运算符重载.您尝试使用+=两种指针类型,这是不允许的 - 如果+=表达式的左侧有指针类型,那么右侧必须是整数类型,结果是指针算术,在这种情况下,这不是你想要的.