Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the double question mark means at the end of the type?

Tags:

arrays

swift

I've faced an unknown type in Show Quick Help section in Playground.

I've opened Show Quick Help section to look at the type of first which is the property of the Array.

The question is, what is the double marks at the end of the type?


This is familiar

Double?

Apple - Optional


Unknown type

Double??

screen-shot

like image 345
emrcftci Avatar asked Mar 04 '20 17:03

emrcftci


People also ask

What is the meaning of double question mark?

If double question marks are used , it is clearly to emphasise something in return, usually from the shock of the previous thing said, to express a strong emotion. For example, if I said:- 'My dog just died' Someone may reply. 'Really??'

Are double question marks rude?

In general, avoid what could be called aggressive punctuation: the combination of multiple consecutive exclamation points and/or question marks (instead of the usual allotment of one) to demonstrate anger, irritation, or urgency. In business communications, such punctuation can be inflammatory or offensive.

What does a question mark mean at the end of a sentence?

A question mark indicates to your readers that your sentence should be read as an inquiry. It is the end mark used in interrogative sentences. Question words like who, when, where, why, what, which, and how indicate that a sentence is a question.

What is the double question mark operator?

In simplest way, two question marks are called "Coalescing Operator", which returns first non null value from the chain. e.g if you are getting a values from a nullable object, in a variable which is not nullable, then you can use this operator.


1 Answers

Double?? is shorthand notation for Optional<Optional<Double>>, which is simply a nested Optional. Optional is a generic enum, whose Wrapped value can actually be another Optional and hence you can create nested Optionals.

let optional = Optional.some(2)
let nestedOptional = Optional.some(optional)

The type of nestedOptional here is Int??.

For your specific example, item.first is Double??, since item itself is of type [Double?] and Array.first also returns an Optional, hence you get a nested Optional.

Your compactMap call on data achieves nothing, since you call it on the outer-array, whose elements are non-optional arrays themselves. To filter out the nil elements from the nested arrays, you need to map over data and then call compactMap inside the map.

let nonNilData = data.map { $0.compactMap { $0 } } // [[100, 35.6], [110, 42.56], [120, 48.4], [200]]
like image 117
Dávid Pásztor Avatar answered Oct 17 '22 19:10

Dávid Pásztor