什么时候Objective-C的隐式转换为BOOL? [英] When does Objective-C implicitly cast to BOOL?

查看:156
本文介绍了什么时候Objective-C的隐式转换为BOOL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了迈克灰的Objective- ç陷阱页面,现在我偏执到 BOOL 键入 ID 的隐式铸造变量。

假设我有一个较低位的危险的指针归零,因此,它强制转换为 BOOL 将产生 NO ,即使它指向一个有效的对象。我们称之为危险的指针

如何做简单的if语句工作?
请问if语句投 BOOL 时,计算该条件?还是将它转换为一个纯粹的布尔?

 如果(富)
{
  [myArray的ADDOBJECT:美孚]。
}

它的工作原理相同的方式在三元前pressions?

  //这是否休息的时候foo是危险?
self.titleLabel.hidden = foo的?是:否;

还是会如果我这样做三元只有突破:

  //我pretty肯定现在打破
self.titleLabel.hidden =((BOOL)富)?是:否;

我觉得我失去了用C基本的东西有关逻辑运算,以及它们如何与 BOOL 。请帮赐教。


解决方案

  

在不Objective-C的隐式转换为BOOL?


从来没有,因为这句话在语义上不正确。强制转换,顾名思义,明确的。什么你问被称为隐式类型转换(强制 - 感谢,乔希!)

此外,严格地说,在Objective-C的条件句不是特殊:他们是从C继承所以前pressions,需要评估为布尔值,不被视为时BOOL ,但作为 INT

  //这是否休息的时候foo是'危险'?'
self.titleLabel.hidden = foo的?是:否;

没有,没有。 (表达式)作为布尔EX pression使用时,相当于(表达式!= 0)。这里没有风险。

  //我pretty肯定现在打破
self.titleLabel.hidden =((BOOL)富)?是:否;

现在这个可以打破,因为 BOOL 只是typedeffed到符号字符,这是在iOS 8位。因此,如果,这是一个(32位)的指针,被截断为8位,并且指针都是零的低8位,但指针本身是不是,这会错误地报告假的。你不想这样做是多余的,丑陋的和危险的演员。如果你想更明确,写

  VAR =(OBJ!=无)? valueOne:valueTwo;

代替。

I read Mike Ash's Objective-C pitfalls page, and now I'm paranoid about implicitly casting variables of type id to BOOL.

Assume I have a 'dangerous' pointer with the lower bits zeroed out, so that casting it to a BOOL would produce NO even though it points to a valid object. Let's call this 'dangerous' pointer foo.

How do simple if statements work? Does the if statement cast foo to a BOOL when it evaluates the condition? Or does it cast to a pure boolean?

if( foo )
{
  [myArray addObject:foo];
}

Does it work the same way in ternary expressions?

// Does this break when foo is 'dangerous'?
self.titleLabel.hidden = foo ? YES : NO ; 

Or will the ternary only break if I do this:

// I'm pretty sure it breaks now
self.titleLabel.hidden = ((BOOL)foo) ? YES : NO ; 

I feel like I'm missing something basic about logical operations in C, and how they relate to BOOL. Please help enlighten me.

解决方案

When does Objective-C implicitly cast to BOOL?

Never, since this phrase is semantically incorrect. A cast is, by definition, explicit. What you're asking about is called an "implicit type conversion" (coercion - thanks, Josh!).

Also, strictly speaking, the conditionals in Objective-C are not special: they're inherited from C. So expressions, when needed to evaluated as booleans, aren't treated as BOOL, but as int.

// Does this break when foo is 'dangerous'?`
self.titleLabel.hidden = foo ? YES : NO;

No, it doesn't. (expr) when used as a "boolean" expression, is equivalent to (expr != 0). No risks here.

// I'm pretty sure it breaks now
self.titleLabel.hidden = ((BOOL)foo) ? YES : NO;

Now this can break, since BOOL is just typedeffed to signed char, which is 8 bit on iOS. Thus, if foo, which is a (32-bit) pointer, gets truncated to 8 bits, and the lower 8 bits of the pointer were all zero, but the pointer itself wasn't nil, this will incorrectly report false. You don't want to do this superfluous, ugly and dangerous cast. If you want to be more explicit, write

var = (obj != nil) ? valueOne : valueTwo;

instead.

这篇关于什么时候Objective-C的隐式转换为BOOL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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