Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Mastering Swift
  • Table Of Contents Toc
Mastering Swift

Mastering Swift

By : Jon Hoffman
5 (2)
close
close
Mastering Swift

Mastering Swift

5 (2)
By: Jon Hoffman

Overview of this book

If you are a developer that learns best by looking at, and working with, code, then this book is for you. A basic understanding of Apple's tools is beneficial but not mandatory.
Table of Contents (17 chapters)
close
close
16
Index

Select a closure based on results


In the final example, we will pass two closures to a method, and then depending on some logic, one, or possibly both, of the closures will be executed. Generally, one of the closures is called if the method was successfully executed and the other closure is called if the method failed.

Let's start off by creating a class that will contain a method that will accept two closures and then execute one of the closures based on the defined logic. We will name this class TestClass. Here is the code for the TestClass class:

class TestClass {
  typealias compareClosure = ((String) -> Void)

  func isGreater(numOne: Int, numTwo:Int, successHandler: compareClosure, failureHandler: compareClosure) {
    if numOne > numTwo {
      successHandler("\(numOne) is greater than \(numTwo)")
    }
    else {
      failureHandler("\(numOne) is not greater than \(numTwo)")
    }

  }
}

We begin this class by creating a type alias that defines the closure that we will use for...

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Mastering Swift
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon