简体   繁体   English

属性观察者和属性包装器之间的主要区别是什么?

[英]What's the main difference between property observers and property wrappers?

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.上面两种情况几乎完成了相同的事情,就是将属性设置为等于或小于 12。

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:您的示例(以及您的一些文本)似乎来自Swift Programming Language: Property Wrapper手册:

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 mo 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.很明显,您也可以编写自己的 setter 和 getter(恕我直言,这比编写自身变异的观察者的模式更好),但是您失去了属性包装器提供的重用和抽象。

You go on to say:你go就说:

The two cases above accomplish pretty much the same thing, which is to set the properties to be equal to or less than 12.上面两种情况几乎完成了相同的事情,就是将属性设置为等于或小于 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.它还将这个“等于或小于 12”逻辑的细节从你声明属性的地方抽象出来。

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.属性观察器和属性包装器之间的另一个重大区别是属性观察器可以访问self ,而属性包装器还不能(在撰写本文时)使用稳定的、记录在案的接口访问self

You can work around this limitation by manually passing self to the property wrapper in init .您可以通过手动将self传递给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.您可以使用未记录的、不稳定的接口在属性包装器中访问self ,您可以通过在您最喜欢的搜索引擎中输入“property wrapper _enclosureInstance”来了解该接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Restkit中的“属性”和“属性”有什么区别 - What's the difference between “attribute” and “property” in restkit 委托属性声明中'weak'和'assign'之间的区别是什么 - What's the difference between 'weak' and 'assign' in delegate property declaration ARC:成员变量和属性之间有什么区别? - ARC: what's the difference between a member variable and property? 运行时属性和实际属性之间的性能差异是什么? - What's the performance difference between runtime property and real properties? SKNode的position属性和GKAgent2d的position属性有什么区别 - What is the difference between SKNode's position property and GKAgent2d's position property ReactiveCocoa和PromiseKit之间的主要区别是什么? - What's the main difference between ReactiveCocoa and PromiseKit? 定义方法的属性时,+和-有什么区别? - What is the difference between + and - when defining a property of method? Swift 中的属性和变量有什​​么区别? - What is the difference between a property and a variable in Swift? 使用方法和属性有什么区别? - What is the difference between using a method and a property? UITableViewController中的View属性和TableView属性有什么区别? - What is the difference between the View property and TableView property in UITableViewController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM