51

プロパティ内でこれを操作する場合、 -とwillSet-の違いは何ですか?didSetgetset

私の観点からは、どちらもプロパティの値を設定できます。いつ、なぜ、willSet- didSet、いつget-を使用する必要がありsetますか?

との構造は次のようにwillSetなりdidSetます。

var variable1 : Int = 0 {
    didSet {
        println (variable1)
    }
    willSet(newValue) {
    ..
    }
}

var variable2: Int {
    get {
        return variable2
    }
    set (newValue){
    }
}
4

5 に答える 5

51

いつ、なぜ willSet/didSet を使用する必要があるのか

  • willSet値が格納される直前に呼び出されます。
  • didSet新しい値が格納された直後に呼び出されます。

出力の例を考えてみましょう:


var variable1 : Int = 0 {
        didSet{
            print("didSet called")
        }
        willSet(newValue){
            print("willSet called")
        }
    }

    print("we are going to add 3")

     variable1 = 3

    print("we added 3")

出力:

we are going to add 3
willSet called
didSet called
we added 3

前/後条件のように機能します

一方、getたとえば読み取り専用プロパティを追加する場合は、次のように使用できます。

var value : Int {
 get {
    return 34
 }
}

print(value)

value = 2 // error: cannot assign to a get-only property 'value'
于 2014-08-20T07:25:43.377 に答える
26

@マキシムの答えは、質問の最初の部分です。

getおよびをいつ使用するかについてsetは、計算されたプロパティが必要な場合。これ:

var x: Int

変数によって自動的にバックアップされる格納されたプロパティを作成します (ただし、直接アクセスすることはできません)。そのプロパティに値を設定することは、プロパティに値を設定することで変換され、取得する場合も同様です。

その代わり:

var y = {
    get { return x + 5 }
    set { x = newValue - 5}
}

変数によってバックアップされない計算されたプロパティを作成します。代わりに、通常は別のプロパティから値を読み書きし、より一般的には計算の結果として、ゲッターおよび/またはセッターの実装を提供する必要があります (したがって、計算されたプロパティ名)

推奨読書:プロパティ

:あなたのコード:

var variable2: Int {
    get{
        return variable2
    }
    set (newValue){
    }
}

は、自分自身を返そうとしているため、間違っています。つまり、再帰的に呼び出すことを意味します。実際、コンパイラは次のようなメッセージで警告します。getgetAttempting to access 'variable2' within its own getter

于 2014-08-20T07:37:31.787 に答える
0

設定する

get-and-setgettable および settable プロパティを実装する場合は、通常の構文を使用できます。ただし、プロパティgetを実装する場合は構文のみを使用できread-onlyます。あなたと一緒に財産setterが与えられnewValueます。

class GetterSetter {
    var theValue: Bool = false
    var property: Bool {
        get { return theValue }
        set {
            print("Value changed from \(theValue) to \(newValue)")
            theValue = newValue
        }
    }
}

let getterSetter = GetterSetter()
getterSetter.property = true
getterSetter.property

// PRINTS:
// Value changed from 'false' to 'true'

したセット

didSet プロパティオブザーバーは、プロパティが設定されたばかりのときにコードを実行する必要がある場合に使用されます。実装すると、前の値を表すdidSetことができます。oldValue

class DidSetter {
    var property: Float16 = -1.0 {
        didSet {
            print("Value changed from \(oldValue) to \(property)")
        }
    }
}

let didSetter = DidSetter()
didSetter.property = 5.0
didSetter.property

// PRINTS:
// Value changed from -1.0 to 5.0

ウィルセット

willSet プロパティオブザーバーは、プロパティが設定される前にコードを実行する必要がある場合に使用されます。実装willSetするとnewValue、新しいプロパティ値を表すことができます。

class WillSetter {
    var property: String = "NO" {
        willSet {
            print("Value changed from \(property) to \(newValue)")
        }
    }
}

let willSetter = WillSetter()
willSetter.property = "YES"
willSetter.property

// PRINTS:
// Value changed from NO to YES
于 2020-12-04T00:28:05.487 に答える