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

后端 未结 3 1760
庸人自扰
庸人自扰 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 07:23

    From Swift 4 onwards you can do:

    func addFilter(newFilter: GPUImageOutput & GPUImageInput)
    

    Further reading: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

    http://braking.github.io/require-conformance-to-multiple-protocols/

    Multiple Type Constraints in Swift

提交回复
热议问题