Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
May 21, 2018, 09:39:33 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
Author Topic: Question from Program 10.3  (Read 2361 times)
Posts: 10

« on: July 03, 2011, 09:02:04 AM »

I was just typing this program out and had a question:

Are the following two statements equal?

if ( days != 0 )
if ( days )

Are there any cases where this logic will fail?  Is there a practical reason to use the first statement beyond readability?

Thanks for any input!
Hero Member
Posts: 3114

« Reply #1 on: July 03, 2011, 11:36:22 AM »

Yes, they're equal.  The logic can fail if a method is defined to return a BOOL value and returns a nonzero value to indicate success instead of the value YES.  That can lead to trouble if a statement like this is written:

Code: (Objective-C)
if ( [myObject testCondition] == YES )

That is, a BOOL method should return either YES or NO.   You can assume all Apple methods will return either of these two values, but be wary of third-party code that may not.


Pages: [1]   Go Up
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 All rights reserved.