From this Stackoverflow Question
Given this example method in Objective C:
- (void)addFilter:(GPUImageOutput <GPUImageInput>*)newFilter;
How to rewrite it in Swift, saying that the input parameter is of Class GPUImageOutput (or subclass), and, at the same time, conforms to the protocol GPUImageInput ?
In Swift, the solution comes from Generics:
Given these example declarations of protocol, main class and subclass:
protocol ExampleProtocol { func printTest() // classes that implements this protocol must have this method }
// an empty test class class ATestClass { }
// a child class that implements the protocol class ATestClassChild : ATestClass, ExampleProtocol { func printTest() { println("hello") } }
Now, you want to define a method that takes an input parameters of type ATestClass (or a child) that conforms to the protocol ExampleProtocol. Write the method declaration like this:
func addFilter<T where T: ATestClass, T: ExampleProtocol>(newFilter: T)
{
println(newFilter)
}
The original method, redefined in Swift, should be
func addFilter<T where T:GPUImageOutput, T:GPUImageInput>(newFilter:T!)
{
// ...
}