Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 18, 2017, 03:55:28 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
  Print  
Author Topic: Question from Program 10.3  (Read 2218 times)
bpcookson
Newbie
*
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 )
-and-
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!
Brian
Logged
skochan
Administrator
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.

Cheers,

Steve
Logged
Pages: [1]   Go Up
  Print  
 
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 classroomM.com. All rights reserved.