1

is there in objective-c something similar to ruby's:

foobar = foo || bar

while foo is nil and bar is 1... so foobar will become 1 or something else if foo wouldn't be nil :)

1 Answer 1

5

Use C's ternary conditional operator:

foobar = foo != nil ? foo : bar;

In general, it takes the form

<var> = <condition to test> ? <true value> : <false value>;

As Wevah noted, if you enable GNU C99 extensions (-std=gnu99) you can also do

foobar = foo ?: bar
Sign up to request clarification or add additional context in comments.

3 Comments

If you've got GCC extensions enabled (e.g., via std=gnu99), you can also do: foobar = foo ?: bar;
@Wevah: post your comment as an answer and I'll give you credit. That is what I was looking for.
@bresc: It's really just expansing on mipadi's answer, though…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.