问号和冒号(?:三元运算符)在objective-c中是什么意思? [英] What does the question mark and the colon (?: ternary operator) mean in objective-c?

查看:29
本文介绍了问号和冒号(?:三元运算符)在objective-c中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这行代码是什么意思?

label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;

?: 把我搞糊涂了.

The ? and : confuse me.

推荐答案

这是 C 三元运算符(Objective-C 是 C 的超集):

This is the C ternary operator (Objective-C is a superset of C):

label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;

在语义上等同于

if(inPseudoEditMode) {
 label.frame = kLabelIndentedRect;
} else {
 label.frame = kLabelRect;
}

没有第一个元素的三元组(例如 variable ?: anotherVariable)与 (valOrVar != 0) 的含义相同?valOrVar : anotherValOrVar

The ternary with no first element (e.g. variable ?: anotherVariable) means the same as (valOrVar != 0) ? valOrVar : anotherValOrVar

这篇关于问号和冒号(?:三元运算符)在objective-c中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆