Swift - Optional Void

I was busy using the NSURLProtocolClientURLProtocol function :

welf?.client?.URLProtocol(welf!, didReceiveResponse: operation.response, cacheStoragePolicy: NSURLCacheStoragePolicy.NotAllowed)

I was expecting a return Void. But, to my surprise, he returnsVoid?

Why is it necessary to distinguish between VoidandVoid?

I read what Voidis a type alias for an empty type. So, does this have anything to do with the difference between an empty type type vs vs nil?

+4
source share
2 answers

This is simply because you are using optional conversations . The method returns Void, but the whole chain can return nilbefore the method is ever called.

, Void , ( ), nil , nil.

+7

, () nil :

let a:Void? = ()
let b:Void? = nil

a == nil // -> false
b == nil // -> true

, , .

let result = welf?.client?.URLProtocol(welf!, didReceiveResponse: operation.response, cacheStoragePolicy: NSURLCacheStoragePolicy.NotAllowed)
if result != nil {
    // success
}
else {
    // `welf?.client` was `nil`
}
+4

All Articles