What does the "?" mean in the following statement
Forgive my "newbie" question, but what the开发者_如何学运维 heck does the question mark, "?" mean in the fololowing line of code?
self.navigationItem.leftBarButtonItem.title = (editing) ?
NSLocalizedString(@"Done", @"Done") : NSLocalizedString(@"Edit", @"Edit");
This is a ternary statement, the ?
is the conditional operator. The statement is basically saying:
if (editing) {
self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"Done", @"Done");
} else {
self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"Edit", @"Edit");
}
You could think of it like:
?
- If previous statement is true, do code immediately after.
:
- Else / Otherwise, run the code immediately after this.
You can read more here http://en.wikipedia.org/wiki/Ternary_operation. You will find that this construct is available in many languages other than C / Objective-C.
It is called ternary operator, it's like an if statement but in one line.
It is like :
if (editing)
self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"Done", @"Done");
else
self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"Edit", @"Edit");
This is the conditional operator: x = a ? b : c
. If a
is true, it assignes b
to x
, otherwise it assigns c
.
It's called the Ternary operation.
What it means is that, given the following code:
(condition) ? a : b
The code will execute a
if condition
is true
, and b
otherwise.
In your case, the code will behave the same as if you were to write:
if (editing) {
self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"Done", @"Done");
} else {
self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"Edit", @"Edit");
}
its a short if statement
in the code below the statement 1 > 0 is checked, if its true the first set will be executed, after the "?" if its false it will run the code behind the ":"
(1 > 0)? true: false;
Ternary operator is used http://www.techotopia.com/index.php/Objective-C_Operators_and_Expressions#The_Ternary_Operator. It means that title will be initialised with NSLocalizedString(@"Done", @"Done") if editing variable is TRUE and with NSLocalizedString(@"Edit", @"Edit") otherwise.
condition ? valueIfTrue : valueIfFalse
This can be read as
if (editing)
self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"Done", @"Done");
else
self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"Edit", @"Edit");
It is ternary operation. Take a look at following explanation in wikipedia — http://en.wikipedia.org/wiki/Ternary_operation#C.2C_C.2B.2B.2C_C.23.2C_Vala.2C_Objective-C.2C_Java.2C_JavaScript.2C_ActionScript
This has obviously been answered (thoroughly) above but I'm throwing my hat in (for the off chance someone just learning programming happens to stumble upon this):
Here is my plain english definition of a ternary statement(what this is)
Variable = what_to_check ? true_value : false_value;
is saying:
is what_to_check true? okay then, set Variable to true_value. If not, set Variable to false_value.
精彩评论