Arik Segal
Arik Segal

Reputation: 3031

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

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! ?

Upvotes: 2

Views: 58

Answers (2)

vadian
vadian

Reputation: 285039

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 (?)

Upvotes: 3

Vollan
Vollan

Reputation: 1915

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

Upvotes: 1

Related Questions