I would like to write a function that takes an array of objects conforming to a given protocol. I wrote the following Playground code:
protocol Request {}
struct RequestA: Request {}
struct RequestB: Request {}
func execute<T: Request>(requests: [T]) {}
let requests: [Request] = [RequestA(), RequestB()]
execute(requests: requests)
However, on the last line I receive this error: "Value of protocol type 'Request' cannot conform to 'Request'; only struct/enum/class types can conform to protocols".
I believe that this error is happening because the function expects an array of objects conforming to the protocol, not an array of the protocols, but I am lost on how to do this another way.
Can someone help sort this out?
Thanks in advance!
T. Change your function to thisfunc execute(requests: [Request]) {}- gcharita