Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

objective c - 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 Ruby's operator ||=

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...