Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to assign a string to a variable before comparing it to another?

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?

like image 631
Bryan Avatar asked Aug 19 '09 22:08

Bryan


People also ask

Can a string be assigned to a variable?

To assign it to a variable, we can use the variable name and “=” operator. Normally single and double quotes are used to assign a string with a single line of character but triple quotes are used to assign a string with multi-lines of character.

Can you assign a value to a variable in Python?

Answer. Yes, variables in Python can be reassigned to a new value that is a different data type from its current value. In fact, variables can be reassigned to any valid value in Python, regardless of its current value.


2 Answers

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  } 
like image 166
Alex Rozanski Avatar answered Sep 19 '22 19:09

Alex Rozanski


if ([statusString isEqualToString:@"Wrong"]) {     // do something } 
like image 43
Wevah Avatar answered Sep 21 '22 19:09

Wevah