Most languages use the true/false
keywords for boolean values. I found that even Smalltalk is using true/false
. I know Objective-C is just borrowing concepts from Smalltalk, not the language itself, but I'm curious why it's using YES/NO
instead of the more widely-used true/false
. Is there any historical reason?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- didBeginContact:(SKPhysicsContact *)contact not in
- Custom Marker performance iOS, crash with result “
- Is it guaranteed that False “is 0” and True “is 1”
Apple have always tried to make things easier to use. If you read some system boolean methods and ask yourself what makes more sense to answer a boolean question with, either using
YES|NO
orTRUE|FALSE
, you'll see thank the answer isYES|NO
in my opinion.Otherwise you can always use
TRUE|FALSE
in your code.The best way to think of this is that it's parallel evolution.
Objective-C's
BOOL
andYES
/NO
dates all the way back to early 1980s, and was likely intended to not only mimic other languages but miss C's future development._Bool
,true
/false
in C were only made part of the standard in 1999.So are
YES
andNO
historical? Yes. Are they only historical? No. Just asNULL
is not the result of 3-3 in a pure sense (despiteNULL
often being defined as0
, or casually usable if it were),true
is not a value forBOOL
.You would not (I think) write this code:
This is less obviously wrong, but it's on the same spectrum:
Objective-C was designed to be (and still is) a strict superset of C. The creators worked very hard to ensure that they did not break compatibility with C in any way. They also tried to make their modifications somewhat obvious so that it would be easy to tell which parts of the code use Objective-C and which parts use plain C. Case in point, the
@
used to denote NSStrings rather than just using quotes. This allows plain C strings to coexist with the new ones.C already had an informal system of TRUE/FALSE macros. I suspect the designers of Objective-C chose the YES/NO macros to avoid conflict and to make it obvious that the code is actually Objective-C. Notice also the usage
nil
for the 'empty' object rather than just modifying the behavior of good old NULL.It is strange, but I find code is more readable using the
YES
/NO
macros rather thanTRUE
/FALSE
(which also work).However, Objective-C is a superset of C99 now, so you should be using the C99
boolean
type andtrue
andfalse
wherever possible. I was toying with the idea of definingyes
andno
totrue
andfalse
but have resisted it so far.Objective-C is a very verbose language, all methods are very descriptive, and using YES/NO for boolean values instead of true/false makes it more human readable.
You would probably find the following conversation strange, if it happened in real life: A: "Did you see the movie?" B: "True"
If B had answered "yes" (or "no"), it would sound perfectly normal, and code looks more like plain english by using YES/NO instead of true/false.