Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8907197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:51:01+00:00 2026-06-15T02:51:01+00:00

I have a condition where I want a view controller to conform to any

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:51:03+00:00Added an answer on June 15, 2026 at 2:51 am

    Sure, you can make an array of protocols:

    NSArray *protocols = @[@protocol(UIApplicationDelegate),
        @protocol(UIImagePickerControllerDelegate),
        @protocol(UIScrollViewDelegate),
        @protocol(NSFileManagerDelegate)];
    

    You could then check that some object conforms to all of them:

    UIViewController *vc = ...;
    for (Protocol *protocol in protocols) {
        if (![vc conformsToProtocol:protocol]) {
            NSLog(@"object doesn't conform to %@", protocol);
        }
    }
    

    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:

    @property (nonatomic, strong) id<UIApplicationDelegate, UIImagePickerController,
        UIScrollViewDelegate, NSFileManagerDelegate> swissArmyKnife;
    

    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.

    // This is probably not such a good idea.
    if ([object conformsToProtocol:@protocol(NSFileManagerDelegate)]) {
        return [object fileManager:myFileManager shouldRemoveItemAtURL:url];
    } else {
       return arc4random_uniform(2);
    }
    
    
    // This is probably better.
    if ([object respondsToSelector:@selector(fileManager:shouldRemoveItemAtURL:)]) {
        return [object fileManager:myFileManager shouldRemoveItemAtURL:url];
    } else {
       return arc4random_uniform(2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have if condition and I want to disable or enable my actionLink button.
I have a written a loop for and if condition and I want to
I have a script that I want to exit early under some condition: if
I have a script that I want only to load if a certain condition
I want to toggle a class based on some condition which I have to
I have a GridView with 10 columns. On a certain condition, I want to
I have a view containing a segmented controller and a UIDatePicker that only shows
I have a UINavigationController with a root view controller and then I push a
I want to move from one view controller to the next, using code. I
i have trouble with calling one view controller to another view controller using segue

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.