Skip to content

Instantly share code, notes, and snippets.

@neelbakshi94
Created March 9, 2021 04:55
class Person {
let name: String
init(name: String) {
self.name = name
}
func introduce() {
print("Hi I am \(name)!")
}
func greet(personName: String) {
print("Hey \(personName). My name is \(name) 😊")
}
}
var person: Person? = Person(name: "Neel")
var greetPerson = person?.greet // printName is of type (String) -> Void which seems like it's referenced as a closure
var performGreeting = { [greetPerson] in
greetPerson?("Ron")
}
greetPerson = nil
person = nil // Even though person here becomes nil, the greetPerson function is captured in the performGreeting closure
performGreeting() // Since greetPerson is captured, it will also capture the person value within the function and this will print out Hey Ron. My name is Neel 😊
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment