Created
March 9, 2021 04:55
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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