swift中双重问号的目的是什么? [英] what's the purpose of double question mark in swift

查看:160
本文介绍了swift中双重问号的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过这样的功能:

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.

推荐答案

它被称为nil coalescing运算符。如果 highs 不是 nil 而是解包并返回值。如果它为零,则返回 [ChartHighlight]()。当可选项 nil 时,这是一种给出默认值的方法。

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.

这篇关于swift中双重问号的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆