What does this line of code mean?
这一行代码是什么意思?
label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;
The ?
and :
confuse me.
的吗?和:混淆我。
13 个解决方案
#1
388
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
没有第一个元素(例如变量?:其他变量)的三元表示与(valOrVar != 0)相同?valOrVar:anotherValOrVar
#2
164
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.
只有在选择值时才计算值。
#3
35
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模式中使用和释放本地变量。增值!
#4
31
Simply, the logic would be
简单地说,逻辑就是
(condition) ? {code for YES} : {code for NO}
(条件)?{代码为YES}:{代码为NO}
#5
13
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.
这就是通常的三元算符。如果问号前的部分为true,则计算并返回冒号前的部分,否则计算并返回冒号后的部分。
a?b:c
is like
就像
if(a)
b;
else
c;
#6
4
This is part of C, so it's not Objective-C specific. Here's a translation into an if
statement:
这是C的一部分,所以它不是特定于Objective-C的。这里是if语句的翻译:
if (inPseudoEditMode)
label.frame = kLabelIndentedRec;
else
label.frame = kLabelRect;
#7
4
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;
#8
1
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;
#9
1
I just learned something new about the ternary operator. The short form that omits the middle operand is truly elegant, and is one of the many reasons that C remains relevant. FYI, I first really got my head around this in the context of a routine implemented in C#, which also supports the ternary operator. Since the ternary operator is in C, it stands to reason that it would be in other languages that are essentially extensions thereof (e. g., Objective-C, C#).
我刚学到了三元运算符的新知识。省略中间操作数的简短形式是真正优雅的,并且是C保持相关的许多原因之一。顺便提一下,我首先是在c#中实现的例程中了解到这一点的,c#也支持三元运算符。由于三元运算符在C语言中,因此可以认为它在其他语言中本质上是它的扩展。objective - C,C #)。
#10
1
As everyone referred that, It is a way of representing conditional operator
正如大家所提到的,它是表示条件运算符的一种方式
if (condition){
true
}
else {
false
}
using ternary operator (condition)? true:false
To add additional information, In swift we have new way of representing it using ??
.
使用三元运算符(条件)?正确:错误添加额外信息,在swift中我们有新的表示方式?
let imageObject: UIImage = (UIImage(named: "ImageName")) ?? (initialOfUsername.capitalizedString).imageFromString
Which is similar to
这是类似于
int a = 6, c= 5;
if (a > c)
{
a is greater
} else {
c is greater
}
is equivalent to
相当于
if (a>c)?a:c
==> Is equal to if (a>c)?:c
如果(> c)?a:c => = if (a>c)
instead of ?:
we can use ??
is swift.
我们可以用?是迅速的。
#11
1
int padding = ([[UIScreen mainScreen] bounds].size.height <= 480) ? 15 : 55;
means
意味着
int padding;
if ([[UIScreen mainScreen] bounds].size.height <= 480)
padding = 15;
else
padding = 55;
#12
0
Ternary operator example.If the value of isFemale boolean variable is YES, print "GENDER IS FEMALE" otherwise "GENDER IS MALE"
三元运算符的例子。如果isFemale布尔变量的值为YES,打印"性别为女性"否则"性别为男性"
? means = execute the codes before the : if the condition is true.
: means = execute the codes after the : if the condition is false.
Objective-C
objective - 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.
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.
#13
0
Fun fact, in objective-c if you want to check null / nil For example:
有趣的事实,在objective-c中如果你想检查空/ 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;
- 如果一个对象是nil,它将返回false作为布尔值;
- Ternary Operator's second parameter can be empty, as it will return the result on the left of '?'
- 三元运算符的第二个参数可以为空,因为它将返回'?'左边的结果。
So let say you write:
假设你写:
[self getSomeString] != nil?: @"";
the second parameter is returning a boolean value, thus a exception is thrown.
第二个参数是返回一个布尔值,因此抛出一个异常。
#1
388
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
没有第一个元素(例如变量?:其他变量)的三元表示与(valOrVar != 0)相同?valOrVar:anotherValOrVar
#2
164
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.
只有在选择值时才计算值。
#3
35
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模式中使用和释放本地变量。增值!
#4
31
Simply, the logic would be
简单地说,逻辑就是
(condition) ? {code for YES} : {code for NO}
(条件)?{代码为YES}:{代码为NO}
#5
13
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.
这就是通常的三元算符。如果问号前的部分为true,则计算并返回冒号前的部分,否则计算并返回冒号后的部分。
a?b:c
is like
就像
if(a)
b;
else
c;
#6
4
This is part of C, so it's not Objective-C specific. Here's a translation into an if
statement:
这是C的一部分,所以它不是特定于Objective-C的。这里是if语句的翻译:
if (inPseudoEditMode)
label.frame = kLabelIndentedRec;
else
label.frame = kLabelRect;
#7
4
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;
#8
1
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;
#9
1
I just learned something new about the ternary operator. The short form that omits the middle operand is truly elegant, and is one of the many reasons that C remains relevant. FYI, I first really got my head around this in the context of a routine implemented in C#, which also supports the ternary operator. Since the ternary operator is in C, it stands to reason that it would be in other languages that are essentially extensions thereof (e. g., Objective-C, C#).
我刚学到了三元运算符的新知识。省略中间操作数的简短形式是真正优雅的,并且是C保持相关的许多原因之一。顺便提一下,我首先是在c#中实现的例程中了解到这一点的,c#也支持三元运算符。由于三元运算符在C语言中,因此可以认为它在其他语言中本质上是它的扩展。objective - C,C #)。
#10
1
As everyone referred that, It is a way of representing conditional operator
正如大家所提到的,它是表示条件运算符的一种方式
if (condition){
true
}
else {
false
}
using ternary operator (condition)? true:false
To add additional information, In swift we have new way of representing it using ??
.
使用三元运算符(条件)?正确:错误添加额外信息,在swift中我们有新的表示方式?
let imageObject: UIImage = (UIImage(named: "ImageName")) ?? (initialOfUsername.capitalizedString).imageFromString
Which is similar to
这是类似于
int a = 6, c= 5;
if (a > c)
{
a is greater
} else {
c is greater
}
is equivalent to
相当于
if (a>c)?a:c
==> Is equal to if (a>c)?:c
如果(> c)?a:c => = if (a>c)
instead of ?:
we can use ??
is swift.
我们可以用?是迅速的。
#11
1
int padding = ([[UIScreen mainScreen] bounds].size.height <= 480) ? 15 : 55;
means
意味着
int padding;
if ([[UIScreen mainScreen] bounds].size.height <= 480)
padding = 15;
else
padding = 55;
#12
0
Ternary operator example.If the value of isFemale boolean variable is YES, print "GENDER IS FEMALE" otherwise "GENDER IS MALE"
三元运算符的例子。如果isFemale布尔变量的值为YES,打印"性别为女性"否则"性别为男性"
? means = execute the codes before the : if the condition is true.
: means = execute the codes after the : if the condition is false.
Objective-C
objective - 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.
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.
#13
0
Fun fact, in objective-c if you want to check null / nil For example:
有趣的事实,在objective-c中如果你想检查空/ 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;
- 如果一个对象是nil,它将返回false作为布尔值;
- Ternary Operator's second parameter can be empty, as it will return the result on the left of '?'
- 三元运算符的第二个参数可以为空,因为它将返回'?'左边的结果。
So let say you write:
假设你写:
[self getSomeString] != nil?: @"";
the second parameter is returning a boolean value, thus a exception is thrown.
第二个参数是返回一个布尔值,因此抛出一个异常。