On Apr 24, 2014, at 7:33 AM, Alex Zavatone <[email protected]> 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 protected])
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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]