Skip to content

Latest commit

 

History

History
39 lines (26 loc) · 608 Bytes

property.md

File metadata and controls

39 lines (26 loc) · 608 Bytes

Default

let name: String = "Kautilya"
var age: Int = 28

age += 1
print("Name: " + name + " Age: " + age)

Lazy Property

struct Person {
    let name: String
    let age: Int
}

struct PeopleViewModel {
    let people: [Person]
    
    lazy var oldest: Person? = {
        print("Lazy var oldest initialized")
        return people.max(by: { $0.age < $1.age })
    }()
    
    init(people: [Person]) {
        self.people = people
        print("View model initialized")
    }
}

avanderlee | lazy-var-property