How would one go about writing a one line if statement in objective-c? Is there a proper style for this?
I know of the ternary operator used for assignment (int a = condition ? b : c
) but say I wanted to call a method is if(condition){ [self methodCall]; }
the recommended way of dealing with this in one line?
In addition are there any objc style guides out there? (Think comparable to this ruby style guide) Coming back to a language after a while of not touching it makes me want to rethink how I style my code.
Ternary if statement (if - else)
condition ? [self methodcall] : [self otherMethodCAll];
Single method call
if (condition) [self methodcall];
The single-line ruby-style if
and unless
statements do not have an equivalent in Objective-C. You can either stick with blocks, or just call the method on the same line:
if (condition) {
[self method];
}
or
if (condition)
[self method];
or
if (condition) [self method];
You don't need brackets...
if(condition) [self methodCall];
That's as succinct as it gets.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With