-
(Swift) Lazy PropertiesiOS💖 2024. 9. 3. 15:47
- 퍼포먼스 측면에서 인스턴스 생성시, 프로퍼티가 쓰이는 시점에 생성하는 방법
- code)
거래 struct의 history에서, Transactions가 20만명에게 3만번 거래를 가지고 있었다면,
Trade struct 를 생성하는데 매번 Transactions를 갖고온다면 부담스럽다.
>> 따라서, 객체가 생성되는 시점이 아닌, 해당 history라는 property에 접근할 때,
그때의 객체 인스턴스가 history 에 접근할때, 해당 인스턴스(lazy붙인 history) 를 생성하고,
history의 타입인 Trancsactions의 initializing 한다.
struct Transactions { init() { print("********History Loading...***********") } } struct Trade { var item: String lazy var history: Transactinos = Transactions() // var history: Transactions = Transactions() init(item: String) { self.item = item } } var newTrade = Trade(item: "AsiaBank") // nenwTrade.history'iOS💖' 카테고리의 다른 글
(Swift) Access Control (2) 2024.09.03 (Swift) Type Properties & Methods (0) 2024.09.03 (iOS) UICollectionViewCell의 awakeFromlib() 는? (1) 2023.01.05 (iOS) UserInterfaceState.xcuserstate, 커밋 해야할까? (1) 2022.12.22 (iOS) ViewController 객체함수 life cycle (0) 2022.12.22