Difference between @propertyDelegate and @propertyWrapper

大憨熊 提交于 2019-12-23 15:25:57

问题


Which is the difference between @propertyWrapper and @propertyDelegate? In all WWDC19 videos they talk about @propertyWrapper, however all actual implementation use @propertyDelegate(i.e SwiftUI - State); the compiler on Xcode 11 Beta seems to accept both and ask for the exact same requirement:

@propertyDelegate struct A {

}
// Property delegate type 'A' does not contain a non-static property named 'value'
@propertyWrapper struct A {

}
// Property delegate type 'A' does not contain a non-static property named 'value'

Note that in both cases the compiler says "Property delegate".

Am I missing something or this is because they didn't decide yet which name to use?


回答1:


TLDR; they are the same but you should use @propertyWrapper.

As pointed out by @JosefDolezal on twitter https://twitter.com/josefdolezal/status/1137619597002248192?s=21, the name @propertyDelegate was returned for revision by the core team https://forums.swift.org/t/returned-for-revision-se-0258-property-delegates/24080. The core team proposed multiple alternative namings, but because the proposal wasnt finalized before WWDC, they chose one of them to introduce this feature to the world.

So its very likely that @propertyDelegate will be removed, and likely that @propertyWrapper will stay, although this could still change during the ongoing evolution process.




回答2:


In the second version of the @propertyWrapper Swift Evolution proposal, there's a section outlining changes from the first reviewed version. At the top of this list, there we have it:

The name of the feature has been changed from "property delegates" to "property wrappers" to better communicate how they work and avoid the existing uses of the term "delegate" in the Apple developer community.

@propertyDelegate has just been renamed to @propertyWrapper. The name @propertyDelegate will vanish soon, although it's still present in the SwiftUI documentation as of 15/06/19.



来源:https://stackoverflow.com/questions/56510768/difference-between-propertydelegate-and-propertywrapper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!