可可 - 切换一个BOOL不重复的名字 [英] Cocoa -- toggling a BOOL without repeating its name

查看:148
本文介绍了可可 - 切换一个BOOL不重复的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个BOOL有一个很好的简称,它是很容易写:

If a BOOL has a nice short name, it's easy enough to write:

myBOOL = !myBOOL;

但是,如果BOOL有很长的名字?

But what if the BOOL has a long name?

objectWithLongishName.memberWithLongishName.submember.myBOOL = !(objectWithLongishName.memberWithLongishName.submember.myBOOL);  

。 。 。看起来不那么pretty。

. . . does not look so pretty.

我不知道是否有来切换BOOL无需输入其名称两次简单的方法?

I'm wondering if there is an easy way to toggle the BOOL without entering its name twice?

推荐答案

下面是另一个:

MyBooleanYaddaYadda ^= YES;

这是有点脆 - 这将打破传统的C code这意味着任何非零的整数计算为真。不过话又说回来,这样将苹果框架code - 我遇到的情况在哪里可可非零,非一INT,当作为BOOL过去了,不会造成同样的效果传递YES

This is kinda brittle - it will break on legacy C code that implies that any nonzero integer evaluates to true. But then again, so will Apple framework code - I encountered cases in Cocoa where a nonzero, non-one int, when passed as a BOOL, would not cause the same effect as passing a YES.

但是,它不依赖的是位模式 - 只在NO是0.这是pretty多一个给定的,考虑到中c间$ P $点整数作为逻辑值。此外,它不承担BOOL的实际数据类型(这对可可符号字符,顺便说一句)。

However, it does not rely on the bit pattern of YES - only on NO being 0. Which is pretty much a given, considering the way C interprets integers as logical values. Also, it does not assume the actual datatype of BOOL (which on Cocoa is signed char, by the way).

是对可可的位模式为1。但是,这不是一个普遍的惯例。在某些平台上没有内置布尔数据类型,充当一个逻辑TRUE整型常量是 1 - 全1。这是为0xFFFFFFFF如果跨preTED为无符号。这种编码具有模糊优点bitwize NOT(在〜中C运算符)等效于逻辑NOT(在C!运算符)。即,〜0xFFFFFFFF的是0,也就是即〜TRUE为FALSE。如果真被定义为1不会这样的。

The bit pattern of YES on Cocoa is 1. But that's not a universal convention. On some platforms with no built-in boolean datatype, the integer constant that serves as a logical TRUE is -1 - all one bits. That's 0xFFFFFFFF if interpreted as unsigned. This coding has a vague advantage that bitwize NOT (the ~ operator in C ) is equivalent to logical NOT (the ! operator in C). That is, ~0xFFFFFFFF is 0, i. e. ~TRUE is FALSE. Doesn't work that way if TRUE is defined as 1.

这篇关于可可 - 切换一个BOOL不重复的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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