Re: Does [myObject valueForString:@"param"] == [NSNull nul] ? @"X":@"Y" mean the same as [myObject valueForString:@"param"] ? @"X":@"Y"
Re: Does [myObject valueForString:@"param"] == [NSNull nul] ? @"X":@"Y" mean the same as [myObject valueForString:@"param"] ? @"X":@"Y"
- Subject: Re: Does [myObject valueForString:@"param"] == [NSNull nul] ? @"X":@"Y" mean the same as [myObject valueForString:@"param"] ? @"X":@"Y"
- From: Graham Cox <email@hidden>
- Date: Wed, 17 Nov 2010 20:56:31 +1100
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 (email@hidden)
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:
This email sent to email@hidden