Create a gist now

Instantly share code, notes, and snippets.

Swift playground example for how/why to use optional let properties in a class
class LetExample {
let name: String?
init(let _ name: String?) {
self.name = name
}
func sayMyName() -> String {
if let myName = self.name {
return "My name is \(myName)"
} else {
return "I am the class with no name"
}
}
}
let example1 = LetExample("Yo Dawg")
let example2 = LetExample(nil)
example1.sayMyName()
example2.sayMyName()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment