How do I specify that a non-generic Swift type should comply to a protocol?

后端 未结 3 1757
庸人自扰
庸人自扰 2020-12-08 06:34

I\'d like to implement a Swift method that takes in a certain class type, but only takes instances of those classes that comply to a specific protocol. For example, in Objec

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 06:59

    this method header from ObjC:

    - (void)addFilter:(GPUImageOutput *)newFilter { ... }
    

    is identical to this header in Swift:

    func addFilter(newFilter: T?) { ... }
    

    both method will accept the same set of classes

    • which is based on GPUImageOutput class; and
    • conforms GPUImageInput protocol; and
    • the newFilter is optional, it can be nil;

提交回复
热议问题