Programming in Swift: Functions & Types

Jan 4 2022 Swift 5.5, iOS 15, Xcode 13

Part 2: Closures

12. Closure Syntax

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions

See course reviews

See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 11. Challenge: Closures Next episode: 13. Challenge: Closure Syntax

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

Now that you’ve got the basic idea, let’s look at couple more special features of closures. Closures are meant to be more lightweight than functions. They’re really great alternatives to functions you wouldn’t get a lot of reuse out of.

let longClosure 
let longClosure 😺= { (a: Int, b: Int) 
}
let longClosure = { (a: Int, b: Int) 😺-> Int in
  a * b
let noParameterTypes
let noParameterTypes😺: Operate
let noParameterTypes: Operate = { (a❌: Int❌, b❌: Int❌) -> Int in
let 😺noReturnType
let noReturnType: Operate = { (a, b) in
let shortClosure: Operate = {...
let shortClosure: Operate = { $0 * $1 }
longClosure(4, 2)
noParameterTypes(4, 2)
noReturnType(4, 2)
shortClosure(4, 2)
let voidClosure: () -> Void
let voidClosure: () -> Void 😺= { () -> Void in
  
}😺
let voidClosure: () -> Void = { ❌() -> Void in❌
  😺print("Yay, Swift!")🛑
}
let voidClosure: () -> 😺()🛑
let voidClosure: 😺Void -> Void🛑
let voidClosure: 😺()🛑 -> Void
voidClosure()
printResult(10, 3, { $0 * $1 + 10 })
printResult(10, 3, operate: (Int, Int) -> Int)
printResult(10, 3) 💛{ (<#Int#>, <#Int#>) -> Int in
  <#code#>
}💛
printResult(10, 3💛) {💛 
printResult(10, 3) { 😺$0 * $1 + 10🛑 }
Button("Add") {
  let priorityIndex = self.taskStore.getIndex(for: self.priority)
  self.taskStore.prioritizedTasks[priorityIndex].tasks.append(
    Task(name: self.text)
  )
        
  self.presentationMode.wrappedValue.dismiss()
}
Button(
  action: { self.modalIsPresented = true }
) {
  Image(systemName: "plus")
}
Button(
  action: { self.modalIsPresented = true }
) {
  Image(systemName: "plus")
}

Button(
  action: { self.modalIsPresented = true },
  label: { Image(systemName: "plus") }
)
init(priority: Task.Priority, names: [String]) {
  self.init(
    priority: priority,
    tasks: names.map 💛{ Task(name: $0) }💛
  )
}