On Thu, 24 Apr 2014 09:44:42 -0700, Jens Alfke said:

>> 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”;

Exactly equivalent if 'x' is a simple thing like a pointer/integer, as being 
discussed.

But as discussed here:

<http://en.wikipedia.org/wiki/%3F:#C>

beware the case of x being an expression and whether it gets evaluated once or 
more times.

Also, the "a = x ? : y;" form is not C, it is a GNU extension.

Cheers,

-- 
____________________________________________________________
Sean McBride, B. Eng                 s...@rogue-research.com
Rogue Research                        www.rogue-research.com 
Mac Software Developer              Montréal, Québec, Canada

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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 arch...@mail-archive.com

Reply via email to