Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between @propertyDelegate and @propertyWrapper

Tags:

swift

swift5

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?

like image 409
Rico Crescenzio Avatar asked Dec 31 '22 22:12

Rico Crescenzio


2 Answers

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.

like image 99
Petr Šíma Avatar answered Jan 03 '23 10:01

Petr Šíma


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.

like image 36
fredpi Avatar answered Jan 03 '23 10:01

fredpi