0

What's the main difference between property observers and property wrappers? They seem to be very similar in that they manage how the properties are stored. The only thing I can think of is that you can reuse property wrappers since there is a layer of separation between code that manages how a property is stored and the code that defines a property.

Property Wrapper

@propertyWrapper
struct TwelveOrLess {
    private var number: Int
    init() { self.number = 0 }
    var wrappedValue: Int {
        get { return number }
        set { number = min(newValue, 12) }
    }
}

struct Rectangle {
    @TwelveOrLess var height: Int
    @TwelveOrLess var width: Int
}

Property Observer

struct Rectangle {
    var height: Int {
        didSet {
            if oldValue > 12 {
                height = 12
            } else {
                height = oldValue
            }
        }
    }

    var width: Int {
        didSet {
            if oldValue > 12 {
                width = 12
            } else {
                width = oldValue
            }
        }
    }
}

The two cases above accomplish pretty much the same thing, which is to set the properties to be equal to or less than 12.

Kevvv
  • 3,655
  • 10
  • 44
  • 90

2 Answers2

1

You say:

The only thing I can think of is that you can reuse property wrappers since there is a layer of separation between code that manages how a property is stored and the code that defines a property.

Your example (and some of your text) appears to be lifted from the Swift Programming Language: Property Wrapper manual:

A property wrapper adds a layer of separation between code that manages how a property is stored and the code that defines a property. For example, if you have properties that provide thread-safety checks or store their underlying data in a database, you have to write that code on every property. When you use a property wrapper, you write the management code once when you define the wrapper, and then reuse that management code by applying it to multiple properties.

So, yes, the virtue of the property wrapper is the reuse achieved by separating the “code that manages how a property is stored and the code that defines a property.” This resulting reuse is the whole m.o. of property wrappers.

You clearly, you can write your own setters and getters (which is better, IMHO, than a pattern of writing an observer that mutates itself), too, but you lose the reuse and abstraction that the property wrappers offer.

You go on to say:

The two cases above accomplish pretty much the same thing, which is to set the properties to be equal to or less than 12.

Sure, but if you want to do this for ten different properties, the wrapper avoids you from needing to repeat this code ten times. It also abstracts the details of this “equal to or less than 12” logic away from where you declare the property.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
1

Another big difference betwixt property observers and property wrappers is that property observers can access self, whilst property wrappers cannot yet (as of this writing) access self using a stable, documented interface.

You can work around this limitation by manually passing self to the property wrapper in init. This workaround is described in the property wrapper proposal.

You can access self in a property wrapper using an undocumented, unstable interface which you can learn about by typing “property wrapper _enclosingInstance” into your favorite search engine.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848