• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Does [myObject valueForString:@"param"] == [NSNull nul] ? @"X":@"Y" mean the same as [myObject valueForString:@"param"] ? @"X":@"Y"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >Does [myObject valueForString:@"param"] == [NSNull nul] ? @"X":@"Y" mean the same as [myObject valueForString:@"param"] ? @"X":@"Y" (From: Devraj Mukherjee <email@hidden>)

  • Prev by Date: Does [myObject valueForString:@"param"] == [NSNull nul] ? @"X":@"Y" mean the same as [myObject valueForString:@"param"] ? @"X":@"Y"
  • Next by Date: Re: What is Mac's "custom" for an agent to display its GUI?
  • Previous by thread: Does [myObject valueForString:@"param"] == [NSNull nul] ? @"X":@"Y" mean the same as [myObject valueForString:@"param"] ? @"X":@"Y"
  • Next by thread: Core Data Document-Based Application with Global Persistent Store
  • Index(es):
    • Date
    • Thread