0

I have been tasked with cleaning up someone else's Objective-C (iOS) code. My Objective-C chops are a bit rusty, so I am hoping you all can verify something for me.

The person who coded the project that I am tasked with cleaning up, seems to be handling a nil object check multiple different ways. I just want to be clear that all of the following actually behave the same way, and there is not some little nuance I am missing.

Method 1

if ([completeProdDict objectForKey:@"specialInstructions"])// does object for key exist 

Method 2

if (nil !=[completeProdDict objectForKey:@"specialInstructions"])// does object for key exist 

Method 3

if ([completeProdDict objectForKey:@"specialInstructions"] !=nil)// does object for key exist 

Is my assumption that all of these behave the same way, correct?

// does object for key exist 

couldn't Method 1 just be used?

erik
  • 4,946
  • 13
  • 70
  • 120
  • 2
    Yes - see http://stackoverflow.com/questions/6177719/testing-for-nil-in-objective-c-ifx-nil-vs-ifx – mservidio Jul 30 '14 at 14:42
  • thank you.. I looked for a duplicate and it didn't come up. thank you – erik Jul 30 '14 at 14:45
  • 1
    it could be. choosing any of them depends on which style is better for your eyes when you read the code. – holex Jul 30 '14 at 14:45

0 Answers0