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.
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With