From the course: Swift 6 Essential Training

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Complex functions

Complex functions

- [Instructor] Functions in Swift can go from simple to complex very quickly. When we start adding in multiple return types, optionals, and even default values. Let's revisit optionals here and add a new function. I'm going to call this setupArenaMatch, with no input parameters, but it is going to return an optional boolean. I'm going to return false here by default, but that can also now hold true or nil. Being the swift programmers that we are, we're going to use our trustee if let statement to unwrap the optional boolean. So we'll say, if let initSuccess, and we just call setupArenaMatch, open and close curly braces and we'll just print out Level initialized and our correctly unwrapped optional. If there's a nil value, we'll handle that too, with else and we'll print out, "Something went terribly wrong." Perfect, now our level initialized is false. We see that in the debug, but we can also change our return value…

Contents