ios swift中双问号的目的是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30837085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what's the purpose of double question mark in swift
提问by Wingzero
I have seen such function:
我见过这样的功能:
public func highlightValues(highs: [ChartHighlight]?)
{
// set the indices to highlight
_indicesToHightlight = highs ?? [ChartHighlight]();
// redraw the chart
setNeedsDisplay();
}
What's the purpose of ??
here? I searched, but it seems searching ??
is hard to find a proper answer.
来??
这里的目的是什么?我搜索过,但似乎搜索??
很难找到正确的答案。
回答by mustafa
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
.
它被称为 nil 合并运算符。如果highs
不是nil
比它打开并返回的值。如果它是零则[ChartHighlight]()
返回。这是一种在可选项为 时给出默认值的方法nil
。