Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Optional Void

I was busy using NSURLProtocolClient's URLProtocol function:

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

I was expecting it to return Void. But to my surprise it returns Void?

Why is it necessary to make a distinction between Void and Void?

I have read that Void is a type alias for the empty tuple type. So, does this have something to do with a distinction between the empty tuple type vs nil?

like image 717
Cloud9999Strife Avatar asked Dec 11 '22 02:12

Cloud9999Strife


1 Answers

This is simply because you are using Optional Chaining. The method returns Void, but it is possible for the whole chain to return nil before the method is ever called.

Essentially, a return value of Void will mean the call was actually made (self and client both have values) while a nil result will mean that one of those were nil.

like image 66
drewag Avatar answered Mar 12 '23 19:03

drewag