双重问号的目的是什么

我见过这样的功能:

public func highlightValues(highs: [ChartHighlight]?)
{
// set the indices to highlight
_indicesToHightlight = highs ?? [ChartHighlight]();


// redraw the chart
setNeedsDisplay();
}

这里的 ??的目的是什么? 我搜索了一下,但似乎很难找到一个正确的答案。

46544 次浏览

It is called nil coalescing operator. If highs is not nil than it is unwrapped and the value returned. If it is nil then [ChartHighlight]() returned. It is a way to give a default value when an optional is nil.