Property type 'MyViewModel' does not match that of the 'wrappedValue' property of its wrapper type

184 Views Asked by At

I have a property wrapper to avoid dealing with implicitly unwrapped optionals.

It works great except when I try to use a custom wrapper to handle weak properties:

@propertyWrapper
public struct WeakMaybeUninitialized<T: AnyObject> {
    
    private weak var storage: AnyObject?
    
    public var wrappedValue: T 
 {
        get { return storage as! T }
        set { storage = newValue }
    }
    
    public init(storage: T? = nil) {
        self.storage = storage
    }
    
    public init() {}
}

Assuming I have the following protocol:

protocol ViewModel: AnyObject {}

Now it looks like this:

weak var viewModel: MyViewModel!

But when I use the property wrapper:

@WeakMaybeUninitialized var viewModel: MyViewModel

It returns the error "Property type 'MyViewModel does not match that of the 'wrappedValue' property of its wrapper type 'WeakMaybeUninitialized'"

Even while MyViewModel actually conforms to AnyObject, so I don't understand what happen.

Do you have a idea why?

0

There are 0 best solutions below