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 3942138
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:37:36+00:00 2026-05-20T00:37:36+00:00

I have come across some strange behavior in my iPhone Objective-C app. I am

  • 0

I have come across some strange behavior in my iPhone Objective-C app.

I am using some code to test an object:

if (!class_conformsToProtocol([someVar someFunctionThatReturnsAClass], @protocol(MyProtocol)))
     [NSException raise:@"Invalid Argument" format:@"The variables returned by 'someFunctionThatReturnsAClass' Must conform to the 'myProtocol' protocol in this case."];

Oddly, when I have a class that looks like this:

@interface BaseClass : NSObject<MyProtocol>

...

@end

@interface SubClass : BaseClass

...

@end

And when I call this fragment: class_conformsToProtocol([SubClass class], @protocol(MyProtocol)), it returns NO.

Also, this code fails:

class_conformsToProtocol([NSString class], @protocol(NSObject)); // also returns NO

While this code returns YES:

[NSString conformsToProtocol:@protocol(NSObject)];

Is there anything I am missing in the docs?
Or is this a bug of some sort? (I am on iOS 4.2 if that matters any).

  • 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-05-20T00:37:36+00:00Added an answer on May 20, 2026 at 12:37 am

    Use NSObject‘s conformsToProtocol: method.

    Here’s an experiment I tried:

    @protocol MyProtocol
    
    - (void) doSomething;
    
    @end
    
    @interface MyClass : NSObject<MyProtocol>
    {
    }
    
    @end
    
    @implementation MyClass
    
    - (void) doSomething { 
    }
    
    @end
    
    @interface MyOtherClass : MyClass
    {
    
    }
    
    @end
    
    @implementation MyOtherClass
    
    - (void) doSomething {
    }
    
    @end
    
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        MyClass *obj_one = [MyClass new];
        BOOL one_conforms = [obj_one conformsToProtocol:@protocol(MyProtocol)];
    
        MyOtherClass *obj_two = [MyOtherClass new];
        BOOL two_conforms  = [obj_two conformsToProtocol:@protocol(MyProtocol)];
        NSLog(@"obj_one conformsToProtocol: %d", one_conforms);
        NSLog(@"obj_two conformsToProtocol: %d", two_conforms);
    
        [pool drain];
        return 0;
    }
    

    Output:

    obj_one conformsToProtocol: 1
    obj_two conformsToProtocol: 1
    

    Whereas:

    MyOtherClass *obj_two = [MyOtherClass new];
    BOOL conforms_two = class_conformsToProtocol([obj_two class], @protocol(MyProtocol));
    NSLog(@"obj_two conformsToProtocol: %d", conforms_two);
    

    Output:

    obj_two conformsToProtocol: 0
    

    Verdict:

    This is a bug with class_conformsToProtocol, use the conformsToProtocol: method of NSObject

    Unlike class_conformsToProtocol, NSObject‘s conformsToProtocol: method will check superclasses as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have come across a strange behavior with Microsoft Code Contracts that I don't
I have come across some strange behaviour, and I'm assuming a bug in Firefox,
I'm currently porting my GAE app to Python 2.7 and have come across some
I have come across an annoying problem while writing some PHP4 code. I renamed
I have taken over some code from a previous developer and have come across
I'm moving some code from Microsoft.Practices.EnterpriseLibrary.Validation.Validators to System.ComponentModel.DataAnnotations, and have come across a more
I have recently come across a situation where code is dynamically loading some libraries,
I have come across a strange defect. I am using Orbeon 3.9 CE on
I have come across some c code where the there is an enum type
Since I started iPhone/iPod Development I have come across some differences between how the

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.