• 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: Good idea/bad idea?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Good idea/bad idea?


  • Subject: Re: Good idea/bad idea?
  • From: Jens Alfke <email@hidden>
  • Date: Thu, 24 Apr 2014 09:44:42 -0700

On Apr 24, 2014, at 7:33 AM, Alex Zavatone <email@hidden> wrote:

> And we messed around a bit looking for any shorthand and though it looked like a terrible idea since the comparison is done against integers using the ternary operator, I'd like to know exactly why it's a terrible idea.
> NSString x;
> x = (x) ?: @"yo";

It’s not a terrible idea; I use this pattern a lot.

The ternary operator doesn’t just work on integers. It’s like “if”, it takes any expression that can be interpreted as a boolean. For a pointer the test is implicitly the same as “x != nil”. The example you gave is exactly equivalent to
	if (x)
		x = x;
	else
		x = @“yo”;

—Jens
_______________________________________________

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


  • Follow-Ups:
    • Re: Good idea/bad idea?
      • From: Sean McBride <email@hidden>
    • Re: Good idea/bad idea?
      • From: Alex Zavatone <email@hidden>
References: 
 >Good idea/bad idea? (From: Alex Zavatone <email@hidden>)

  • Prev by Date: Re: Mouse cursors and overlapping sibling NSViews
  • Next by Date: Re: Mouse cursors and overlapping sibling NSViews
  • Previous by thread: Re: Good idea/bad idea?
  • Next by thread: Re: Good idea/bad idea?
  • Index(es):
    • Date
    • Thread