CocoaUser
CocoaUser

Reputation: 1429

Does get{} and set{} only for computed properties?

I have a question about property in Swift.
Does get{} and set{} only support the computed properties?

var name: String {

  get {
     // return something
  }

  set {
    //  do something
  }
}

Upvotes: 0

Views: 110

Answers (1)

rob mayoff
rob mayoff

Reputation: 385500

Your question is still unclear (even though you added the missing verb). Nevertheless I'll try to answer it:

  1. Only computed properties may have get and set blocks. That is what makes them “computed”.

  2. The opposite of a computed property is a “stored” property. (The language reference calls this a “stored variable property”.) A stored property cannot have get and set blocks.

  3. You can observe changes to stored properties, and to inherited properties of any kind, by defining willSet and didSet blocks for them.

Many details are spelled out in “Properties” in The Swift Programming Language. You can also search for “Properties” in the “Declarations” section of language reference. (Interior links into the language reference don't work as of this writing.)

Upvotes: 2

Related Questions