I want to compare the value of an NSString
to the string "Wrong". Here is my code:
NSString *wrongTxt = [[NSString alloc] initWithFormat:@"Wrong"];
if( [statusString isEqualToString:wrongTxt] ){
doSomething;
}
Do I really have to create an NSString for "Wrong"?
Also, can I compare the value of a UILabel
's text
to a string without assigning the label value to a string?
No, why not just do:
Using
@""
simply creates a string literal, which is a validNSString
.Yes, you can do something like:
You can also use the NSString class methods which will also create an autoreleased instance and have more options like string formatting:
Brian, also worth throwing in here - the others are of course correct that you don't need to declare a string variable. However, next time you want to declare a string you don't need to do the following:
Although the above does work, it provides a retained NSString variable which you will then need to explicitly release after you've finished using it.
Next time you want a string variable you can use the "@" symbol in a much more convenient way:
This will be autoreleased when you've finished with it so you'll avoid memory leaks too...
Hope that helps!