Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the purpose of double question mark in swift [duplicate]

Tags:

ios

swift

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.

like image 940
Wingzero Avatar asked Jun 15 '15 03:06

Wingzero


People also ask

What is the use of double question marks in Swift?

Double question mark is a nil-coalescing operator. In plain terms, it is just a shorthand for saying != nil . First it checks if the the return value is nil, if it is indeed nil, then the left value is presented, and if it is nil then the right value is presented.

What are double question marks used for?

The double question mark is also used in cases when we want to highlight something while asking. Example: “Is that your cupboard or a pile of garbage??” “Didn't you hear me when I said let's go??” and “Who steals a pen??” Note:The sentences we end with a question mark (?) are also called interrogative sentences.

What is the question mark used for in Swift?

Question marks after a type refer to Optionals , a way in Swift which lets you indicate the possibility that a value might be absent for any type at all, without the need for special constants.

What is double question mark in typescript?

Double question marks(??) or nullish coalescing operator helps us to assign default values to null or undefined variables in Angular and Typescript. It's often called as Null coalescing operator. It's introduced as part of Typescript 3.7 version.


1 Answers

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.

like image 110
mustafa Avatar answered Oct 24 '22 20:10

mustafa