Re: Good idea/bad idea?
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