For Swift, is Double? the same as Optional<Double>? -


    var title: double? = nil     var title2 = optional<double>.none 

the 2 things above seem both behave optional doubles. when hold option , click on title , title2, shows have different types. 1 double? , other optional<double>. i'm wondering if theres difference between two. , if aren't different, why have 2 of them? optional objective c thing got transferred on swift or something?

there no difference, , there nothing special double here.

for any type t, t? (compiler built-in) shortcut optional<t>. so

var value: t? var value: optional<t> 

are equivalent. optional conforms nilliteralconvertible protocol, i.e. value can instantiated literal nil, and

var value: t? = nil var value: t? = .none var value: t? = optional.none var value: t? = optional<t>.none var value: t? = t?.none 

are equivalent. in first 3 statements, type optional<t> of value on right-hand side inferred type annotation on left-hand side. last 2 statements can shortened to

var value = optional<t>.none var value = t?.none 

because type inferred automatically right-hand side.

finally, since optionals implicitly initialized .none, all of above statements equivalent.


Comments

Popular posts from this blog

Combining PHP Registration and Login into one class with multiple functions in one PHP file -

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -