1 of 6

ECS 189E: Android and iOS

Closures

Sam King, October 24th 2018

2 of 6

Administrative

Last time: finish up multiple navigation controllers

This time: closures

Next time: start our OS focus

End of Advanced Swift and iOS basics! Congratulations!

3 of 6

Administrative

Quiz on Friday will cover:

  • Closures
  • Navigation and multiple view controllers

4 of 6

Closures and the designer’s goals

  • Compact syntax
  • Let the compiler do work for you
    • Maintain strong type safety
  • Easy to understand
  • Clear semantics

Basically the opposite of function pointers in C or regular expressions

5 of 6

Two different types of closures

  • Named and nested function
  • Anonymous function, also called a lambda in other languages or Closure Expression in swift

General rule when reasoning about closures -- remember what the compiler knows and use that to infer what’s going on in your mind

6 of 6

Break into groups and tell me (1) if this is correct and if so what it does or (2) if this is incorrect why it’s incorrect

let _ = someArray.sorted(by: >)