Programming in Swift: Functions & Types

Jan 4 2022 Swift 5.5, iOS 15, Xcode 13

Part 2: Closures

12. Closure Syntax

Episode complete

Play next episode

Next
About this episode
See versions

Leave a rating/review

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

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Pro subscription. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

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.

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) }💛
  )
}