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

Viewed 669

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

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]]
Related