I have a condition where I want a view controller to conform to any of 4 protocols.
Is there a way to check if it conforms to any of these 4 protocols without doing a bunch of or statements in my if?
Can you make an array of protocols?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sure, you can make an array of protocols:
You could then check that some object conforms to all of them:
It’s hard to imagine why you’d want to do this at run-time, though.
Perhaps what you really want is to declare that something conforms to several protocols. You can do that too, and the compiler will check it for you at compile-time. For example:
If you try to assign something to this property, and it doesn’t conform to all four protocols, the compiler will issue a warning.
Perhaps what you are saying is you want to verify that an object conforms to at least one of the protocols, but that it doesn’t have to conform to all of them. In that case, you have to check at run-time. But that smells like a bad design to me.
If you want to send a message to the object, but you’re not sure that it will understand the message, it’s probably better to check specifically for the message you want to send, instead of checking for protocol conformance.