我想将 NSString
的值与字符串“Wrong”进行比较。这是我的代码:
NSString *wrongTxt = [[NSString alloc] initWithFormat:@"Wrong"];
if( [statusString isEqualToString:wrongTxt] ){
doSomething;
}
我真的必须为“错误”创建一个 NSString 吗?
另外,我可以在不将标签值分配给字符串的情况下将 UILabel
的 text
的值与字符串进行比较吗?
最佳答案
Do I really have to create an NSString for "Wrong"?
不,为什么不这样做:
if([statusString isEqualToString:@"Wrong"]){
//doSomething;
}
使用 @""
只是创建一个字符串文字,它是一个有效的 NSString
。
Also, can I compare the value of a UILabel.text to a string without assigning the label value to a string?
是的,你可以这样做:
UILabel *label = ...;
if([someString isEqualToString:label.text]) {
// Do stuff here
}
关于objective-c - 是否有必要在将字符串与另一个变量进行比较之前将其分配给变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1302985/