https://www.hackingwithswift.com/100/swiftui/9
100 Days of SwiftUI – Day 9 – Hacking with Swift
100 Days of SwiftUI The 100 Days of SwiftUI is a free collection of videos, tutorials, tests, and more to help you learn SwiftUI faster. Click here to learn more, or watch the video below. Back to 100 Days of SwiftUI
www.hackingwithswift.com
Initializer와 Access Level을 보니 잠시 혼란스러웠습니다. struct는 memberwise initializer를 기본으로 제공합니다. 하지만 custom initializer를 구현하게 되면 memberwise initializer가 제공되지 않습니다. property 중 선언과 동시에 초기값을 제공하는 property가 있다면, 기본 제공되는 memberwise initializer의 signature가 늘어납니다.
struct Student {
var name: String = "Unknown"
var age: Int
var wow: Bool = false
}
let student1 = Student(name: "K", age: 100, wow: true)
let student2 = Student(name: "Ian", age: 20000)
let student3 = Student(age: 7, wow: true)
let student4 = Student(age: 50_000_000)
한편, private으로 선언된 property를 갖고 있는 경우, 초기값 정의 여부와 상관없이 memberwise initializer가 제공되지 않습니다. 처음 Swift를 공부할 때 생성자가 참 어렵고 복잡하다는 생각을 했었는데, 새삼스럽게 차근차근 설명을 들어보니 참 좋네요.
Day11. Protocols and extensions. (0) | 2020.01.01 |
---|---|
Day 10. Classes. (0) | 2019.12.31 |
Day 8. Structs, part one. (0) | 2019.12.28 |
Day7. Closures, part two. (0) | 2019.12.28 |
Day 6. Closures, part one. (0) | 2019.12.27 |
댓글 영역