C语言 问号和冒号(?:三元运算符)在objective-c中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2595392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the question mark and the colon (?: ternary operator) mean in objective-c?
提问by danielreiser
What does this line of code mean?
这行代码是什么意思?
label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;
The ?and :confuse me.
在?和:迷惑我。
回答by Barry Wark
This is the C ternary operator(Objective-C is a superset of C):
这是 C三元运算符(Objective-C 是 C 的超集):
label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;
is semantically equivalent to
在语义上等同于
if(inPseudoEditMode) {
label.frame = kLabelIndentedRect;
} else {
label.frame = kLabelRect;
}
The ternary with no first element (e.g. variable ?: anotherVariable) means the same as (valOrVar != 0) ? valOrVar : anotherValOrVar
没有第一个元素(例如variable ?: anotherVariable)的三元表示与(valOrVar != 0) ? valOrVar : anotherValOrVar
回答by Sean
It's the ternary or conditional operator. It's basic form is:
它是三元或条件运算符。它的基本形式是:
condition ? valueIfTrue : valueIfFalse
Where the values will only be evaluated if they are chosen.
如果选择它们的位置,则只能评估它们。
回答by Varun Goyal
Simply, the logic would be
简单地说,逻辑是
(condition) ? {code for YES} : {code for NO}
(condition) ? {code for YES} : {code for NO}
回答by Bruno Bronosky
Building on Barry Wark's excellent explanation...
基于巴里沃克的出色解释......
What is so important about the ternary operator is that it can be used in places that an if-else cannot. ie: Inside a condition or method parameter.
三元运算符的重要之处在于它可以用在 if-else 不能用的地方。即:在条件或方法参数内。
[NSString stringWithFormat: @"Status: %@", (statusBool ? @"Approved" : @"Rejected")]
...which is a great use for preprocessor constants:
...这是预处理器常量的一个很好的用途:
// in your pch file...
#define statusString (statusBool ? @"Approved" : @"Rejected")
// in your m file...
[NSString stringWithFormat: @"Status: %@", statusString]
This saves you from having to use and release local variables in if-else patterns. FTW!
这使您不必在 if-else 模式中使用和释放局部变量。FTW!
回答by Brian
That's just the usual ternary operator. If the part before the question mark is true, it evaluates and returns the part before the colon, otherwise it evaluates and returns the part after the colon.
那只是通常的三元运算符。如果问号前的部分为真,则计算并返回冒号前的部分,否则计算并返回冒号后的部分。
a?b:c
is like
就好像
if(a)
b;
else
c;
回答by Dietrich Epp
This is part of C, so it's not Objective-C specific. Here's a translation into an ifstatement:
这是 C 的一部分,所以它不是 Objective-C 特定的。以下是if声明的翻译:
if (inPseudoEditMode)
label.frame = kLabelIndentedRec;
else
label.frame = kLabelRect;
回答by Claus Broch
It's just a short form of writing an if-then-else statement. It means the same as the following code:
这只是编写 if-then-else 语句的一种简短形式。它的含义与以下代码相同:
if(inPseudoEditMode)
label.frame = kLabelIndentedRect;
else
label.frame = kLabelRect;
回答by handiansom
Ternary operator example.If the value of isFemale boolean variable is YES, print "GENDER IS FEMALE" otherwise "GENDER IS MALE"
三元运算符示例。如果 isFemale 布尔变量的值为 YES,则打印“GENDER IS FEMALE”,否则打印“GENDER IS MALE”
? means = execute the codes before the : if the condition is true.
: means = execute the codes after the : if the condition is false.
Objective-C
目标-C
BOOL isFemale = YES; NSString *valueToPrint = (isFemale == YES) ? @"GENDER IS FEMALE" : @"GENDER IS MALE"; NSLog(valueToPrint); //Result will be "GENDER IS FEMALE" because the value of isFemale was set to YES.
BOOL isFemale = YES; NSString *valueToPrint = (isFemale == YES) ? @"GENDER IS FEMALE" : @"GENDER IS MALE"; NSLog(valueToPrint); //Result will be "GENDER IS FEMALE" because the value of isFemale was set to YES.
For Swift
对于斯威夫特
let isFemale = false let valueToPrint:String = (isFemale == true) ? "GENDER IS FEMALE" : "GENDER IS MALE" print(valueToPrint) //Result will be "GENDER IS MALE" because the isFemale value was set to false.
let isFemale = false let valueToPrint:String = (isFemale == true) ? "GENDER IS FEMALE" : "GENDER IS MALE" print(valueToPrint) //Result will be "GENDER IS MALE" because the isFemale value was set to false.
回答by BananZ
Fun fact, in objective-c if you want to check null / nil For example:
有趣的事实,在objective-c 中如果你想检查null / nil 例如:
-(NSString*) getSomeStringSafeCheck
{
NSString *string = [self getSomeString];
if(string != nil){
return String;
}
return @"";
}
The quick way to do it is:
快速的方法是:
-(NSString*) getSomeStringSafeCheck
{
return [self getSomeString] != nil ? [self getSomeString] : @"";
}
Then you can update it to a simplest way:
然后您可以将其更新为最简单的方式:
-(NSString*) getSomeStringSafeCheck
{
return [self getSomeString]?: @"";
}
Because in Objective-C:
因为在 Objective-C 中:
- if an object is nil, it will return false as boolean;
- Ternary Operator's second parameter can be empty, as it will return the result on the left of '?'
- 如果一个对象为 nil,它将返回 false 作为布尔值;
- 三元运算符的第二个参数可以为空,因为它将返回'?'左边的结果。
So let say you write:
所以假设你写:
[self getSomeString] != nil?: @"";
the second parameter is returning a boolean value, thus a exception is thrown.
第二个参数返回一个布尔值,因此抛出异常。
回答by cdhw
It is ternary operator, like an if/else statement.
它是三元运算符,就像 if/else 语句一样。
if(a > b) {
what to do;
}
else {
what to do;
}
In ternary operator it is like that: condition ? what to do if condition is true : what to do if it is false;
在三元运算符中,它是这样的:条件 ? 如果条件为真怎么办:如果条件为假怎么办;
(a > b) ? what to do if true : what to do if false;

