4

Something that has piqued my interest is Objective-C's BOOL type definition.

Why is it defined as a signed char (which could cause unexpected behaviour if a value greater than 1 byte in length is assigned to it) rather than as an int, as C does (much less margin for error: a zero value is false, a non-zero value is true)?

The only reason I can think of is the Objective-C designers micro-optimising storage because the char will use less memory than the int. Please can someone enlighten me?

Andy Bowskill
  • 1,734
  • 3
  • 18
  • 36
  • 1
    Just don't assign anything to it other than YES and NO, and when you test it, just test whether it is set or not. That is the guideline so there should be no issue with whatever silly type they choose for whatever silly reason they chose it ;) – Jason Coco Apr 05 '10 at 22:42

1 Answers1

6

Remember that Objective-C was created back in the 1980's, when saving bytes really mattered.

As mentioned in a comment, as long as you stick with the values YES and NO, everything will be fine.

Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
  • That's a great point Kristopher, with all the relatively recent attention on iPhone/iPad development it is easy to forget that Objective-C is a lot older than the platforms it is currently being implemented on. – Andy Bowskill Apr 05 '10 at 23:21