Swift: Property conforming to a specific class and in the same time to multiple protocols

前端 未结 3 683
生来不讨喜
生来不讨喜 2020-12-05 04:22

In Objective-C, it\'s possible to write something like that:

@property(retain) UIView *myView;

But how can

3条回答
  •  情书的邮戳
    2020-12-05 05:11

    In Swift 4 it's finally possible. You can declare variable of some class conforming to protocol at the same time, like this:

    class ClassA {
        var someVar: String?
    }
    
    protocol ProtocolA {}
    
    class ClassB {
        var someOptional: (ClassA & ProtocolA)? // here is optional value
        var some: ClassA & ProtocolA // here is non-optional value; need to provide init though :)
    }
    

提交回复
热议问题