85

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?

1
  • By the way, you're wasting time with initWithFormat, you can change NSString *wrongTxt = [[NSString alloc] initWithFormat:@"Wrong"]; to NSString *wrongTxt = @"Wrong"; Only bother with initWithFormat if you're going to try to calculate a string from other inputs e.g. NSString *wrongTxt = [[NSString alloc] initWithFormat:@"%@ was wrong %d times",craigString, wrongCount];
    – Craig
    Jul 13, 2012 at 3:14

4 Answers 4

178

Do I really have to create an NSString for "Wrong"?

No, why not just do:

if([statusString isEqualToString:@"Wrong"]){
    //doSomething;
}

Using @"" simply creates a string literal, which is a valid NSString.

Also, can I compare the value of a UILabel.text to a string without assigning the label value to a string?

Yes, you can do something like:

UILabel *label = ...;
if([someString isEqualToString:label.text]) {
    // Do stuff here 
}
2
  • That works. I guess I just have buggy code. It was throwing an exception before.
    – Bryan
    Aug 19, 2009 at 22:39
  • 1
    To elaborate: label.text IS a string, so of course you don't need to create a string from it for the comparison.
    – Amagrammer
    Aug 20, 2009 at 12:52
26
if ([statusString isEqualToString:@"Wrong"]) {
    // do something
}
1
  • Thanks wevah. Perspx was 1 sec faster.
    – Bryan
    Aug 19, 2009 at 22:41
8

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:

NSString *myString = [[NSString alloc] initWithFormat:@"SomeText"];

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:

NSString *myString = @"SomeText";

This will be autoreleased when you've finished with it so you'll avoid memory leaks too...

Hope that helps!

3
  • I was wondering about that. I often see string variables that are objective c objects, but they are never released. Thanks!
    – Bryan
    Aug 20, 2009 at 0:10
  • 2
    The hint is in the "alloc" bit. If you use an Alloc (or Copy) then you have to specifically release. If you don't, the convention is that the object will be already set to autorelease.
    – h4xxr
    Aug 20, 2009 at 8:18
  • Yes, although it can contain new or copy for example newObject (from NSDictionaryController) or mutableCopy (from NSObject) developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/… Aug 20, 2009 at 8:54
2

You can also use the NSString class methods which will also create an autoreleased instance and have more options like string formatting:

NSString *myString = [NSString stringWithString:@"abc"];
NSString *myString = [NSString stringWithFormat:@"abc %d efg", 42];

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.