Git Product home page Git Product logo

swift-closure-tutorial's Introduction

Swift-Closure-Tutorial

A short introduction to closures in practical usage.

Objectives

  • Understand what a closure is
  • Identify closures used commonly in UIKit
  • Write closures yourself
  • Create system that makes paractical use of a closure

Lesson

Hands on.

Outline

mins activity description
5 Intro What is a closure?
5 Questions
5 Playground Sample code
10 Exercise Closures in the wild
5 Discussion
5 Exercise Write closure types
5 Exercise ButtonNode
10 Exercsie Joystick
5 Discussion Capturing values
10 Exercise Iterator

Intro

Closures are self-contained blocks of functionality. Sometimes closures are called blocks, lambdas, or anonymous functions in other languages.

If you are familiar with functions than you should understand closures with little effort. In Swift functions are a special case of closure. By learning closures you will expand your ability to use functions into new and useful territory. You will also develop a stronger understanding of the built frameworks.

In a nustshell closures are just the functions that you are already familiar with the following added features:

  • Closures can be passed as values
  • Closures can be assigned to variables
  • Closures capture (or enclose) variables from the surrounding context

Question:

  • Assuming everyone is familiar with functions explain a function?
  • Who is familar with closures?
  • Given the features above suggest some use cases for Closures?

What do Closures look like in Swift?

Closures Example:

Playground: Closures1.playground

Exercise: Math functions. Student directed.

Closure Types

Closures are values and as such they have a type. The Type of a closures is it's signature. The signature is the parameters it accepts, and the value it returns.

var a = {(Int) -> Int in /* do stuff */} // This clsoure is type: (Int) -> (Int)

You could declare a var of this type that could be assigned the closure above:

var f: (Int) -> (Int)

Capturing Values

Closures can capture variables that exist outside their local scope. You could say a closure encloses these values. Which might explain the name?

Example Files: Closures2.playground

Important! closures capture the value of parameters passed to a function that creates a closure.

Exercise: Iterator! Make an iterator. This will be a function that takes an array as a parameter, and returns a closure that will iterate the contents of the array. See the notes in the playground for details. Students work in pairs to solve this problem.

Post exercise discussion: Everyone pair up and decide what is most important and what is most confusing about capturing values.

Closures in UIKit

Activity: Closures in the Wild. Download and open the project below. Find the closures in the sample project. How many did you find?

Project: ClosuresExample1

The closures used in this project might be referred to as "callbacks". A callback is a block of code passed to another part of your program to execute at a more convenient time.

Closures are values. Take a look at the callback in presentViewController(). A quick look at the signature of a function.

Exercise

Add a new UIAlertAction. The handler parameter is a closure! Write the code block to set the background color to red when this button is tapped. (Don't forget to add your alert action to the alert!)

Further challenges:

  • When closing the alert show the user name in the label
  • Make the new action show a view or label that slides up from the bottom of the screen.
  • Make the new action display second alert dialog with three text fields.

Discussion Questions

  • What do the closures do here?
  • Does presentViewController() use a closure?
  • Take a close look at: addTextFieldWithConfigurationHandler() what does the closure do here?
  • Can you compare closures to the delegate pattern?

Exercise:

Write the function types for the closures in the example, do this with a partner.

  • UIAlertAction( ... handler:) (the handler param)
  • presentViewController(... completion:) (The completion param)
  • addTextFieldWithConfigurationHandler()

After you've written the type look up the documentation for each of these functions and compare.

Use cases closures

Closures are useful when you want to add functionality to something, or need the flexibility to decide functionality as convenient.

In the case of presentViewController "completion" might be referred to as a "callback" since it's execution is asynchronous. The animation must complete before the aniamtion is finished.

With UIAlertAction handler is a block of code that may or may not be executed at some point in the future. We call this use a handler since the closure is how we are handling user interaction with a button.

Discussion

  • Why does UIKit use closures for UIAlertAction and presentViewController()?
  • Why does addTextFieldWithConfigurationHandler() use a closure?
  • Why do you have to use self in closures?

MSButtonNode

The MSButtonNode class is another use case for closures. The selectedHandler is a class property the value of which is a function that takes no parameters and returns nothing.

Project file: Closure example 2

Exercise: Write a closure to make the button do something.

Challenge: Make an alternative to the ButtonNode class call it: ToggleButtonNode. Toggle button will display two states like a switch or a check box. It will change states with each tap and execute one of two closures alternating.

Passing Values to a Closure

In the previous example you made use of UIKit objects and methods that make use of closures. In this example you will add a closure to an existing class.

The Joystick class is used with SpriteKit. It creates a virtual joystick that appears on the screen. Your goal is to add a new property to this class that will take two parameters: x and y. Joystick should invoke this closure passing it's xValue, and yValue. Use the closure to move the box.

Project files: Closure Example 3

Conclusion

Closures are really useful. They are used extensively in the UIKit framework. A function is a closure. Think of closures as blocks of fucntional code that can be assigned, passed, and returned.

Exercise:

  • List the features of closures?
  • Contrast the difference between using a closure and the delegate pattern?

Vocabulary

Here are some terms used when talking about closures

Function literal

A function/closure assigned to variable. This is sometimes called a "function literal"

var a = {(Int) -> Int in /* do stuff */}

Higher order Functions

A higher order function is a function that takes another function as a parameter.

Callback

A closure passed to an object. Usually executed Asynchronously.

Handler

A closure executed by another object, usually in response to an action, like a tap, swipe, or other event/action.

Function

Another name for a Closure (In swift Functions are a special case of closure)

Block

Another name for closure.

swift-closure-tutorial's People

Contributors

soggybag avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.