开发者

Short way to write conditional assignment in ObjC

Does Objective-C have an even shorter way of writing this line of code?

 a = b ? b : c;

That is, a way to say, a is equal to b as long it is not nil, else c. This is like 开发者_Python百科Ruby's operator ||=


Does the following work for you:

a = b ? : c;

(This syntax is a GNU extension to C, so you might have to use the GCC and not LLVM - http://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Conditionals.html#Conditionals).


That's as short as you can get it in Objective-C! That's a nice little test you have there.

The only other short way I could come up with is as follows (I wouldn't recommend it for readability reasons and it isn't as short as yours!):

if (b) a = b; else a = c;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜