这行代码是什么意思?
label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;
和?
混淆:
我。
这行代码是什么意思?
label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;
和?
混淆:
我。
这是 C三元运算符(Objective-C 是 C 的超集):
label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;
在语义上等价于
if(inPseudoEditMode) {
label.frame = kLabelIndentedRect;
} else {
label.frame = kLabelRect;
}
没有第一个元素的三元(例如variable ?: anotherVariable
)的含义与(valOrVar != 0) ? valOrVar : anotherValOrVar
它是三元或条件运算符。它的基本形式是:
condition ? valueIfTrue : valueIfFalse
只有在选择它们时才会评估这些值。
简单地说,逻辑是
(condition) ? {code for YES} : {code for NO}
基于 Barry Wark 的出色解释...
三元运算符的重要之处在于它可以用在 if-else 不能用的地方。即:在条件或方法参数内。
[NSString stringWithFormat: @"Status: %@", (statusBool ? @"Approved" : @"Rejected")]
...这对预处理器常量很有用:
// in your pch file...
#define statusString (statusBool ? @"Approved" : @"Rejected")
// in your m file...
[NSString stringWithFormat: @"Status: %@", statusString]
这使您不必在 if-else 模式中使用和释放局部变量。FTW!
这是 C 的一部分,所以它不是 Objective-C 特定的。这是一个声明的翻译if
:
if (inPseudoEditMode)
label.frame = kLabelIndentedRec;
else
label.frame = kLabelRect;
这只是编写 if-then-else 语句的一种简短形式。它的含义与以下代码相同:
if(inPseudoEditMode)
label.frame = kLabelIndentedRect;
else
label.frame = kLabelRect;
有趣的事实,在 Objective-C 中如果你想检查 null / nil 例如:
-(NSString*) getSomeStringSafeCheck
{
NSString *string = [self getSomeString];
if(string != nil){
return String;
}
return @"";
}
快速的方法是:
-(NSString*) getSomeStringSafeCheck
{
return [self getSomeString] != nil ? [self getSomeString] : @"";
}
然后您可以将其更新为最简单的方式:
-(NSString*) getSomeStringSafeCheck
{
return [self getSomeString]?: @"";
}
因为在 Objective-C 中:
所以假设你写:
[self getSomeString] != nil?: @"";
第二个参数返回一个布尔值,因此抛出异常。
三元运算符示例。如果 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
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.
对于斯威夫特
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.
它是三元运算符,类似于 if/else 语句。
if(a > b) {
what to do;
}
else {
what to do;
}
在三元运算符中是这样的:条件?如果条件为真怎么办:如果条件为假怎么办;
(a > b) ? what to do if true : what to do if false;
int padding = ([[UIScreen mainScreen] bounds].size.height <= 480) ? 15 : 55;
方法
int padding;
if ([[UIScreen mainScreen] bounds].size.height <= 480)
padding = 15;
else
padding = 55;
我刚刚学到了一些关于三元运算符的新知识。省略中间操作数的简短形式确实很优雅,并且是 C 保持相关性的众多原因之一。仅供参考,我首先在 C# 中实现的例程的上下文中真正了解了这一点,该例程也支持三元运算符。由于三元运算符在 C 语言中,因此理所当然地认为它将在本质上是其扩展的其他语言中(例如,Objective-C、C#)。
正如大家所说,它是一种表示条件运算符的方式
if (condition){
true
}
else {
false
}
使用三元运算符(condition)? true:false
来添加额外的信息,在 swift 中我们有新的方式来使用??
.
let imageObject: UIImage = (UIImage(named: "ImageName")) ?? (initialOfUsername.capitalizedString).imageFromString
这类似于
int a = 6, c= 5;
if (a > c)
{
a is greater
} else {
c is greater
}
相当于
if (a>c)?a:c
==> 等于if (a>c)?:c
而不是?:
我们可以使用??
的是 swift。