理由不使用_Bool在Objective-C? [英] Reasons not to use _Bool in Objective-C?

查看:173
本文介绍了理由不使用_Bool在Objective-C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于C99,C现在有一个正确的布尔类型, _Bool 。 Objective-C的,为C严格的超集,继承了这一点,但它是在20世纪80年代创建回来的时候,有没有C布尔类型,所以Objective-C的定义 BOOL 符号字符

Since C99, C now has a proper Boolean type, _Bool. Objective-C, as a strict superset of C, inherits this, but when it was created back in the 1980s, there was no C Boolean type, so Objective-C defined BOOL as signed char.

所有可可用途 BOOL 一样,所有非NEXT /苹果可可code,我已经看到。显然,对于与现有的协议(例如, -applicationShouldTerminateAfterLastWindowClosed: NSApplicationDelegate )的兼容性,匹配已声明的类型preferable,如果没有其他原因,而不是避免警告。

All of Cocoa uses BOOL, as does all non-NeXT/Apple Cocoa code that I've seen. Obviously, for compatibility with existing protocols (e.g., -applicationShouldTerminateAfterLastWindowClosed: from NSApplicationDelegate), matching the already-declared type is preferable, if for no other reason than to avert a warning.

有关清洁/可读性目的, stdbool.h 定义布尔的代名词_Bool ,所以我们这些谁都不希望在我们的code不必要的下划线可以使用。

For cleanliness/readability purposes, stdbool.h defines bool as a synonym for _Bool, so those of us who don't want unnecessary underscores in our code can use that.

另外三个有用的注意事项:

Three other useful notes:


  • @恩code(_Bool)计算结果为B。 ( @恩code(BOOL)计算结果为C签署焦

  • 的sizeof(_Bool)计算结果为 1 ,从C99的定义,得出 _Bool 只有大如要举行两个可能的值。 (修改其实,标准只说,它必须是足够大,以持有这两个值,它不把一个上限,而且,事实上,<一个href=\"http://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/LowLevelABI/100-32-bit_PowerPC_Function_Calling_Conventions/32bitPowerPC.html#//apple_ref/doc/uid/TP40002438-SW21\">Mac在32位的PowerPC OS X将其定义为4个字节。大小差异可能下文件的另一件事 BOOL -vs .- 布尔兼容性问题。)

  • 在这一点,仅有的两个可能值的 _Bool 是1和0。其他任何值转换为这些执行任务之一,因为如果你做了双反( !! )或测试不平等,反对0(!= 0 )。唯一的方式获得 _Bool 与其他值是通常magicks:指针别名和工会

  • @encode(_Bool) evaluates to "B". (@encode(BOOL) evaluates to "c", for signed char.)
  • sizeof(_Bool) evaluates to 1, which follows from C99's definition that _Bool is only as large as necessary to hold its two possible values. ( Actually, the standard says only that it must be "large enough" to hold those two values; it does not place an upper bound, and, in fact, Mac OS X on 32-bit PowerPC defines it as 4 bytes. Size difference is another thing to file under possible BOOL-vs.-bool compatibility issues.)
  • On that note, the only two possible values of a _Bool are 1 and 0. Any other values are converted to one of these on assignment, as if you had done a double-negation (!!) or tested inequality against 0 (!= 0). The only ways to get a _Bool with some other value are the usual magicks: Pointer aliasing and unions.

是否有任何理由不以新的code使用 _Bool / 布尔

Is there any reason not to use _Bool/bool in new code?

推荐答案

我觉得你所有,但回答了你自己的问卷不要在新的可可code使用_Bool的原因是,直到苹果到改变其框架使用_Bool(或者更可能的是,在stdbool.h定义的布尔),你通过使用_Bool或布尔打破惯例,并可能兼容性(至少在没有黑客)。虽然我只在Cocoa编程的一对夫妇几年来浸泡,我敢打赌,如果苹果采用_Bool可言,他们可能会简单地重新定义BOOL宏反正使用新型幕后,避免难言编辑他们的框架和文档。

I think you all but answered your own question- the reason not to use _Bool in new Cocoa code is that, until Apple changes its frameworks over to using _Bool (or more probably, the bool defined in stdbool.h), you're breaking convention and possibly compatibility (at least without hacks) by using _Bool or bool. Although I've only been steeping in Cocoa programming for a couple years now, I'd bet that if Apple incorporates _Bool at all, they will probably simply redefine the BOOL macro to use the new type behind the scenes anyway, to avoid untold editing to their framework and its documentation.

话虽这么说,(让我preface这个由否认说我还没有与Objective-C的混合C code和不知道这样做的惯例),你有一个使用中的C函数新_Bool,可能与它仅供内部使用,不问一个Objective-C的方法在_Bool通过,只是为了避免为未来的程序员困惑警告好得多情况。你想也当然必须坦然面对总是需要C99编译,里面的人可能仍然有理由避免。考虑到是针对1号和宏为0宏,那里似乎没有多大的涨幅,需要℃的较新版本,以获得只使用1或其它字符大小的值0。

That being said, (and let me preface this by disclaiming that I've yet to mix C code in with Objective-C and don't know the conventions for doing so), you have a much better case for using the new _Bool within C functions, probably with the caveat that it is only used internally and doesn't ask an Objective-C method to pass in a _Bool, just to avoid confusion for future programmers. You'd also of course have to be comfortable with always requiring C99 compilation, which people may still have reason to avoid. Considering YES is a macro for 1 and NO is a macro for 0, there doesn't seem to be much gain in requiring a newer version of C to get another char-sized value that only uses 1 or 0.

老实说,当它发生的时候,你可以避开上述任何原因有足够的两轮牛车或限制放置在可重用性,但最终的理由是,它不是可可/ Objective-C的俚语(目前)的一部分,它的好处可能不会超过可读性损失和/或其他不太厕所到_Bool程序员阅读你的code的补充混乱。

Honestly, when it comes down to it, you can get around any of these reasons with enough hackery or restrictions placed on reusability, but the end justification is that it's not (currently) part of the Cocoa/Objective-C slang, and its benefits probably won't outweigh the loss in readability and/or added confusion of other less-privy-to-_Bool programmers reading your code.

这篇关于理由不使用_Bool在Objective-C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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