Amazon.com Widgets Question from Program 10.3
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 12:38:52 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Program Examples
| | |-+  Chapter 10
| | | |-+  Question from Program 10.3
Pages: [1]   Go Down
Print
Author Topic: Question from Program 10.3  (Read 711 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: 3106







« 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:  



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.