https://docs.swift.org/swift-book/LanguageGuide/Properties.html
- ストアドプロパティ
- Lazy Stored Property
- コンピューテッドプロパティ
var width = 0, height = 0, depth = 0
var volume: Double {
return width * height * depth
}
- インスタンスプロパティ
- スタティックプロパティ
- クラスプロパティ
- Property Observers
class StepCounter {
var totalStep: Int = 0 {
willSet(newTotalSteps) {
print("\(newTotalSteps)")
}
didSet {
if totalSteps > oldValue {
print("Added \(totalSteps - oldValue) steps")
}
}
}
}
- Property Wrapper
@propertyWrapper
struct TwelveOrLess {
private var number = 0
var wrappedValue: Int {
get { return number }
set { number = min(newValue, 12) }
}
}
struct SmallRectangle {
@TwelveOrLess var height: Int
@TwelveOrLess var width: Int
}
var rectangle = SmallRectangle()
print(rectangle.height)
// Prints "0"
rectangle.height = 10
print(rectangle.height)
// Prints "10"
rectangle.height = 24
print(rectangle.height)
// Prints "12"
- 再代入の可否による分類
- var
- let
- 値の保持有無による分類
- ストアドプロパティ
- didSet {} or willSet {}
- コンピューテッドプロパティ
- get {} or set {}
- 紐づく対象による分類
- インスタンスプロパティ
- スタティックプロパティ
- クラスプロパティ
- class keyword
- need computed property syntax
- available override that class property