「Swift」クラスにwillSetとdidSetでアクセス修飾子(fileprivate )を利用する

構文
1.fileprivate var 変数
willSet(xx){
some code
}
didSet{
some code
}
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class DemoInfo {
fileprivate var counter: Int = 0{
willSet(newTotal){
print("AA Value: \(newTotal)")
}
didSet{
if counter > oldValue {
print("BB Value \(counter - oldValue)")
}
}
}
}
let cft = DemoInfo()
cft.counter = 100
cft.counter = 800
class DemoInfo { fileprivate var counter: Int = 0{ willSet(newTotal){ print("AA Value: \(newTotal)") } didSet{ if counter > oldValue { print("BB Value \(counter - oldValue)") } } } } let cft = DemoInfo() cft.counter = 100 cft.counter = 800
class DemoInfo {
    fileprivate var counter: Int = 0{
        willSet(newTotal){
            print("AA Value: \(newTotal)")
        }
        didSet{
            if counter > oldValue {
                print("BB Value \(counter - oldValue)")
            }
        }
    }
}
 
let cft = DemoInfo()
cft.counter = 100
cft.counter = 800

実行結果
AA Value: 100
BB Value 100
AA Value: 800
BB Value 700

Swift

Posted by arkgame