A fairly common pattern is: if some object exists, I want to use that object in my code, but if it is not set, I want to fail over to some default value.
Using a background image as an example, and assuming some UIImage *backgroundImage
property that may or may not be set when this code is evaluated, the verbose way to do this might be:
UIImage *image;
if (self.backgroundImage) {
image = self.backgroundImage;
} else {
image = [UIImage imageNamed:@"default"];
}
A more compact method is:
UIImage *image = (self.backgroundImage) ? self.backgroundImage : [UIImage imageNamed:@"default"];
My question is: is there an even more short-hand way to accomplish this?
Something like:
UIImage *image = self.backgroundImage || [UIImage imageNamed:@"default"];
I can't find anything in the Clang Literals documentation, but this sort of short-hand may not actually be considered a 'Literal', or even be part of Clang for that matter.
(I'm also unsure where the "(truth statement) ? id1 : id2" syntax is documented for Clang/LLVM; knowing where the exhaustive set of those short-hand statements are would also be grand!)