@propertyDelegate和@propertyWrapper之间的区别

Ric*_*zio 4 swift swift5.1

@propertyWrapper和之间有@propertyDelegate什么区别?他们在所有WWDC19视频中都谈到@propertyWrapper,但是所有实际的实现方式都使用@propertyDelegate(即SwiftUI-State);编译器Xcode 11 Beta似乎接受两者并要求完全相同的要求:

@propertyDelegate struct A {

}
// Property delegate type 'A' does not contain a non-static property named 'value'
Run Code Online (Sandbox Code Playgroud)
@propertyWrapper struct A {

}
// Property delegate type 'A' does not contain a non-static property named 'value'
Run Code Online (Sandbox Code Playgroud)

请注意,在两种情况下,编译器均会显示“属性委托”

我是否想念某些东西,或者是因为他们尚未决定使用哪个名称?

小智 5

TLDR;它们是相同的,但您应该使用@propertyWrapper

正如@JosefDolezal在Twitter https://twitter.com/josefdolezal/status/1137619597002248192?s=21所指出的那样,该名称@propertyDelegate已由核心团队https://forums.swift.org/t/returned- for-revision-se-0258-property-delegates / 24080。核心团队提出了多种替代命名方式,但是由于该提议尚未在WWDC之前完成,因此他们选择了其中一种来向世界介绍此功能。

因此,很可能@propertyDelegate会删除它,并且很可能@propertyWrapper会保留下来,尽管在进行中的演进过程中仍然可能会改变。