What values should I use for iOS boolean states?(我应该为 iOS 布尔状态使用什么值?)
问题描述
在 iOS 中,我似乎有许多适合布尔值的选项:
It appears that in iOS I have a number of options that seem to fit for boolean values:
YES
NO
TRUE
FALSE
true
false
我应该使用哪些?在这种特殊情况下,我隐藏了一个标签,所以我应该将 hidden 属性设置为 YES、TRUE 或 true代码>?
Which ones should I use? In this particular case I'm hiding a label, so should I set the hidden property to YES, TRUE, or true?
推荐答案
简答:你应该更喜欢 YES 和 NO 来设置 BOOL 类型的基础属性.
Short answer: you should prefer YES and NO for setting foundation properties of type BOOL.
对于长答案,让我们先看看这些常量是在哪里定义的:
For the long answer, let's first see where are these constants defined:
true和false来自stdbool.h;它们是#define-d as1和0TRUE和FALSE来自CFBase.h;它们是#define-d as1和0YES和NO来自NSObjCRuntime.h.这就是signed char是typedef-ed 为BOOL的地方,它的两个值是#define-d 为((BOOL)1)和((BOOL)0)或__objc_yes/__objc_no如果objc_bool支持.
trueandfalseare fromstdbool.h; they are#define-d as1and0TRUEandFALSEare fromCFBase.h; they are#define-d as1and0YESandNOare fromNSObjCRuntime.h. This is wheresigned charistypedef-ed asBOOL, and its two values are#define-d as((BOOL)1)and((BOOL)0)or__objc_yes/__objc_noifobjc_boolis supported.
基础类始终使用 BOOL,它是 signed char 的 typedef,来表示其布尔属性.由于前两对被转换为 int 常量,使用它们可能会导致警告,尽管它可能会正常工作.但是,YES 和 NO 常量以最适合您的编译器的方式定义,无论其版本如何.因此,我建议在整个代码中始终使用 YES 和 NO.
The foundation classes consistently use BOOL, which is a typedef for signed char, to represent its boolean properties. Since the first two pairs get converted to int constants, using them may result in warnings, though it would probably work correctly anyway. The YES and NO constants, however, are defined in the most compatible way for your compiler, regardless of its version. Therefore, I would recommend using YES and NO consistently throughout your code.
这篇关于我应该为 iOS 布尔状态使用什么值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我应该为 iOS 布尔状态使用什么值?
基础教程推荐
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
