定义自己的属性包装器时,必须指定一个名为'wrappedValue'的非静态属性。。例如:
@propertyWrapper
struct MyPropertyWrapper<Value> {
var value: Value
}
编译器上面的代码抱怨:
属性包装器类型“ MyPropertyWrapper”不包含 名为“ wrappedValue”的非静态属性
因此,您可以简单地解决该错误:
@propertyWrapper
struct MyPropertyWrapper<Value> {
var value: Value
var wrappedValue: Value {
get {
value
}
set {
value = newValue
}
}
}
仅举一些例子,这对于@State
是正确的:
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
@propertyWrapper public struct State<Value> : DynamicProperty {
/// The current state value.
public var wrappedValue: Value { get nonmutating set }
}
与@Binding
相同:
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
@propertyWrapper @dynamicMemberLookup public struct Binding<Value> {
/// The value referenced by the binding. Assignments to the value
/// will be immediately visible on reading (assuming the binding
/// represents a mutable location), but the view changes they cause
/// may be processed asynchronously to the assignment.
public var wrappedValue: Value { get nonmutating set }
}
等等。但是@Published
属性包装器没有包装的值:
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
@propertyWrapper public struct Published<Value> {
/// Initialize the storage of the Published property as well as the corresponding `Publisher`.
public init(initialValue: Value)
/// A publisher for properties marked with the `@Published` attribute.
public struct Publisher : Publisher {
/// The kind of values published by this publisher.
public typealias Output = Value
/// The kind of errors this publisher might publish.
///
/// Use `Never` if this `Publisher` does not publish errors.
public typealias Failure = Never
/// This function is called to attach the specified `Subscriber` to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
public func receive<S>(subscriber: S) where Value == S.Input, S : Subscriber, S.Failure == Published<Value>.Publisher.Failure
}
/// The property that can be accessed with the `$` syntax and allows access to the `Publisher`
public var projectedValue: Published<Value>.Publisher { mutating get }
}
我在这里肯定会缺少一些东西,因为如果没有wrappedValue
,编译器将不允许您创建属性包装器。在这种情况下,与其他属性包装器有什么区别?
答案 0 :(得分:1)
您显示的代码不是@Published
的实际实现-仅仅是公开可见的界面。如果将代码粘贴到Xcode中,您将得到与wrappedValue
相同的错误;该代码无法编译。
wrappedValue
的访问级别为internal
-因此我们可以假设@Published
的实际实现声明了类似interval var wrappedValue:Value
的东西。这满足了属性包装器的要求,但意味着wrappedValue
在其框架之外不可见。