On 17/11/2010, at 8:24 PM, Devraj Mukherjee wrote:

> Does these two things mean the same?
> 
> [myObject valueForString:@"param"] == [NSNull nul] ? @"X":@"Y"
> [myObject valueForString:@"param"] ? @"X":@"Y"


No.

First, I assume you mean -valueForKey:, not -valueForString:, plus it's +null, 
not +nul. Pedantry aside, the comparisons are still not equivalent, even 
allowing for the logical inversion you have introduced. 

In the second case, @"X" will be chosen if the expression evaluates to true. 
That will be when the value returned is NOT nil. In the first case, @"X" will 
be chosen when the expression evaluates to equal the address (pointer) of the 
object [NSNull null]. Since that's a real, concrete object, it will be some 
valid address, not nil.

If you want @"X" to be chosen if the value is nil OR [NSNull null], then the 
correct expression would be just that:

if([myObject valueForKey:key] == nil || [myObject valueForKey:key] == [NSNull 
null])
    return @"X";
else
    return @"Y";

You could write this using the tertiary operator syntax, but since it's clearly 
confusing you, I'd stick with if/else.


--Graham


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to