Home > Blockchain >  What is the point of using an "Implicitly Unwrapped Optional" as a function parameter type
What is the point of using an "Implicitly Unwrapped Optional" as a function parameter type

Time:11-04

I go over some code written by other developers and now and then I encounter signatures like this one:

func didReceiveLogMessage(_ message: String!)

Would it be safe to convert this parameter type to String instead of String! ?

CodePudding user response:

Basically never declare a custom function parameter as implicit unwrapped optional. Don't.

This is a pre Swift 3 legacy syntax only for Objective-C and Core Foundation compatibility.

It's absolutely safe and even highly recommended to remove the exclamation mark. However if you really need an optional use a regular optional (?)

CodePudding user response:

This gives no value. It actually adds complexity as even an Inmplicitly unwrapped optional as such counts as an optional, meaning it can be nil (but will crash). a regular String can't be nil

  • Related