How to define a protocol to include a property with @Published property wrapper

后端 未结 6 559
情话喂你
情话喂你 2020-12-15 09:42

When using @Published property wrapper following current SwiftUI syntax, it seems very hard to define a protocol that includes a property with @Published, or I definitely ne

6条回答
  •  无人及你
    2020-12-15 10:32

    You have to be explicit and describe all synthetized properties:

    protocol WelcomeViewModel {
        var person: Person { get }
        var personPublished: Published { get }
        var personPublisher: Published.Publisher { get }
    }
    
    class ViewModel: ObservableObject {
        @Published var person: Person = Person()
        var personPublished: Published { _person }
        var personPublisher: Published.Publisher { $person }
    }
    

提交回复
热议问题