I met with this code, and I could not figure out what does this two question marks mean?
我遇到了这个代码,我无法弄清楚这两个问号是什么意思?
the Definition of this variable looks like following:
此变量的定义如下所示:
var featureImageSizeOptional: CGSize?
The code that makes me confused is:
令我困惑的代码是:
let featureImageSize = featureImageSizeOptional ?? CGSizeZero
2 个解决方案
#1
11
It's the coalescing operator. It returns the first expression (featureImageSizeOptional
) if it's non-nil. If the first expression is nil, the operator returns the second expression (CGSizeZero
).
这是合并运营商。如果它是非零的,它返回第一个表达式(featureImageSizeOptional)。如果第一个表达式为nil,则运算符返回第二个表达式(CGSizeZero)。
See the Language Guide for more info.
有关详细信息,请参阅语言指南。
#2
3
The operator does not 'return the first expression' - it returns the unwrapped value of the first expression (if it is not nil). From the Apple documentation (emphasis mine):
运算符不会'返回第一个表达式' - 它返回第一个表达式的展开值(如果它不是nil)。从Apple文档(强调我的):
The nil coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil. The expression a is always of an optional type. The expression b must match the type that is stored inside a.
如果a包含值,则nil合并运算符(a ?? b)展开可选a;如果a为n,则返回默认值b。表达式a始终是可选类型。表达式b必须与存储在a中的类型匹配。
The nil coalescing operator is shorthand for the code below:
nil合并运算符是以下代码的简写:
a != nil ? a! : b
a!=零?一个! :b
The code above uses the ternary conditional operator and *forced unwrapping (a!) to access the value wrapped inside a when a is not nil*, and to return b otherwise. The nil coalescing operator provides a more elegant way to encapsulate this conditional checking and unwrapping in a concise and readable form.
上面的代码使用三元条件运算符和*强制解包(a!)来访问包含在a不是nil *时的值,否则返回b。 nil合并运算符提供了一种更简洁的方式来以简洁易读的形式封装此条件检查和展开。
#1
11
It's the coalescing operator. It returns the first expression (featureImageSizeOptional
) if it's non-nil. If the first expression is nil, the operator returns the second expression (CGSizeZero
).
这是合并运营商。如果它是非零的,它返回第一个表达式(featureImageSizeOptional)。如果第一个表达式为nil,则运算符返回第二个表达式(CGSizeZero)。
See the Language Guide for more info.
有关详细信息,请参阅语言指南。
#2
3
The operator does not 'return the first expression' - it returns the unwrapped value of the first expression (if it is not nil). From the Apple documentation (emphasis mine):
运算符不会'返回第一个表达式' - 它返回第一个表达式的展开值(如果它不是nil)。从Apple文档(强调我的):
The nil coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil. The expression a is always of an optional type. The expression b must match the type that is stored inside a.
如果a包含值,则nil合并运算符(a ?? b)展开可选a;如果a为n,则返回默认值b。表达式a始终是可选类型。表达式b必须与存储在a中的类型匹配。
The nil coalescing operator is shorthand for the code below:
nil合并运算符是以下代码的简写:
a != nil ? a! : b
a!=零?一个! :b
The code above uses the ternary conditional operator and *forced unwrapping (a!) to access the value wrapped inside a when a is not nil*, and to return b otherwise. The nil coalescing operator provides a more elegant way to encapsulate this conditional checking and unwrapping in a concise and readable form.
上面的代码使用三元条件运算符和*强制解包(a!)来访问包含在a不是nil *时的值,否则返回b。 nil合并运算符提供了一种更简洁的方式来以简洁易读的形式封装此条件检查和展开。