【问题标题】:Understanding method returning BOOL by using !=使用 != 了解返回 BOOL 的方法
【发布时间】:2011-08-25 23:04:45
【问题描述】:

这是一个关于视图控制器shouldAutoRotateToInterfaceOrientation 方法中的return 语句语法的相当基本的问题。

为了允许除倒置纵向模式之外的所有视图,我实现了以下代码块:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

return 语句到底在做什么?我知道它正在返回一个布尔变量,但是它如何确定是返回真还是假?这是 return 语句中的一种隐式 if 语句吗? IE。会:

-    (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown)
        return YES;
}

技术上是一样的,只是更明确地说明?

感谢您的澄清!

【问题讨论】:

    标签: objective-c ios return-value boolean


    【解决方案1】:

    (something != something_else) 这样的比较结果是BOOL 值。如果比较结果为真,则表达式(....) 采用值YES(与TRUE 相同)。

    这不是隐式转换,而是比较的工作方式。

    【讨论】:

    • 哦,哇,这很有道理,我以前从未见过这种语法,所以它让我头晕目眩。谢谢!
    猜你喜欢
    • 2016-05-27
    • 2019-07-08
    • 2016-04-29
    • 2015-06-30
    • 2021-12-20
    • 2016-02-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    相关资源
    最近更新 更多