在JavaScript中,|| operator将返回第一个非错误评估操作数。例如:
var x = null;
var y = "yup";
var z = x || y; // z is now "yup"
是否有与此相同的Objective-C?
例如:
NSString *x = nil;
NSString *y = @"yup";
NSString *z = x ... y; // z should now be @"yup", if "..." were an operator
请注意:'||'在JavaScript中短路
答案 0 :(得分:3)
C(以及ObjC)的编译器扩展可以满足您的需求:
z = x?:y;
(对于标准C / ObjC版本:z = x?x:y)