2

In Java, you can use instanceof to check if a class extends another class or implements an interface.

In Objective-C, you can use isKindOfClass to check if a class extends another class:

if ([myObject isKindOfClass:[AnClass class]]) { }

But how can I check if a class gets extended by a category?


EDIT 2

My code of the first EDIT was unfortunately a bit confusing and nonsensical, sorry! Now, here is my new code:

I'll explain the whole problem:
I've got a class ViewCustomerCreate thats extends UITableViewController. ViewCustomerCreate gets extended by the category ICheckBox. This is my code that doesn't work:

- (void)closeModalView {
    UINavigationController *parent = (UINavigationController *)self.navigationController.parentViewController;
    UIViewController *parentViewContr = parent.topViewController;
    
    if ([parentViewContr isKindOfClass:[id<ICheckBox> class]]) { // ERROR-MESSAGE see below
        id<ICheckBox> parent2 = (id<ICheckBox>)parentViewContr; // works fine :-)
        [parent2 setSelectedElementId:checkedIndex]; // works fine :-)
    }   
    
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

The error message is: "error: 'id' is not an Objective-C class name or alias"

I think that I can't use isKindOfClass to check if a class gets extended by a category, isn't it?

PS: What do I want? I have a general modal view with checkboxes and if I close this view, the parent-view should get informed what the user choose.


EDIT 3

OMG, I confounded Category with Protocol!! Aaaaahhhhh ^^

THE SOLUTION:

if ([parentViewContr conformsToProtocol:@protocol(ICheckBox)]) {
4
  • Your edit doesn't really make it clear what you want. I have no idea why that line doesn't work (does the property not exist? Does it exist but have a buggy implementation? What?), and you don't explain what that line of code has to do with a category. Manni's suggestion to check for methods before calling them seems like the best approach.
    – Chuck
    Feb 10, 2011 at 10:39
  • @chuck, that was my suggestion, Manni is the questioner ;)
    – Jasarien
    Feb 10, 2011 at 10:41
  • You're right, Jasarien and Chuck... Give me some minutes to edit my question a second time... I'll try some changes in my code...
    – Manni
    Feb 10, 2011 at 11:02
  • Thank you very much for you help! I edit my question a second time.
    – Manni
    Feb 10, 2011 at 12:35

2 Answers 2

6

There is no way to check if a class is extended by a category, but you can check whether or not an instance responds to a particular selector with:

- (BOOL)respondsToSelector:(SEL)sel;

In Objective-C you should worry less about what an object is, and worry more about what an object can do.

If it walks like a duck, sounds like a duck and looks like a duck, then it can probably fly, you know what I mean?

You should use this as such:

if ([myObject respondsToSelector:@selector(myMethod:)])
{
    // do whatever you need to do
}

Just a quick note, since you mentioned Java interfaces. You can check if an object implements a protocol (similar to Java interfaces, but not exactly the same) by using:

- (BOOL)conformsToProtocol:(Protocol *)aProtocol;
6
  • Thanks for your answer! I edit my question and explained the whole problem.
    – Manni
    Feb 10, 2011 at 10:33
  • Categories aren't like subclasses, you don't have an instance of a category. A Category adds its methods to an already existing class. If you add a category to NSObject all NSObject instances will have the new methods. My answer still stands. If you explain why it "doesn't work" and tell me what warning you get or what errors, I can help you, but as it stands, my answer is what you should be doing. Also in the future, instead of waiting for an answer and then editing your question to explain the "whole" problem, explain the "whole" problem to start with.
    – Jasarien
    Feb 10, 2011 at 10:39
  • Thank you very much, Jasarien! I updated my question, now. My first edit was a bit confusing, sorry...
    – Manni
    Feb 10, 2011 at 12:36
  • @Manni, if ([parentViewContr isKindOfClass:[id<ICheckBox> class]]) is total nonsense :) -- the id <ICheckBox> is checking whether id implements a protocol, which is nonsense because id isn't an instance of any object. Like I keep saying, you need to use the method in my answer. Categories are not protocols. You cannot check them the same way.
    – Jasarien
    Feb 10, 2011 at 12:59
  • Categories only add methods to classes, they do not extend them in the same way subclassing does. As a consequence, a category is not a class so it cannot be used in checks such as isKindOfClass:. Please understand this.
    – Jasarien
    Feb 10, 2011 at 13:03
0

If you have defined a category on UIViewController, there are no instances of UIViewController that it is not applied to. Hence, a runtime check does not make sense.

Let's look at your actual problem:

parent.setMySpecialValue = 1; // DOES NOT WORK :-(

What does "DOES NOT WORK" actually mean? Do you get a compiler error or a runtime error. If the former, there are a couple of possible issues to loo at:

  1. You haven't included the header file containing the category in the source file that uses that method
  2. It is a property that you have named incorrectly. If the property is called mySpecialValue, that line of code should read:

    parent.mySpecialValue = 1;
    

    or

    [parent setMySpecialValue: 1];
    

As of now, categories cannot define instance variables, so having a synthesized property might be an issue, so that might also be your problem, but you need to give more information about what "DOES NOT WORK" means.

1
  • Thank you very much, JeremyP! I updated my question, now. My first edit was a bit confusing, sorry...
    – Manni
    Feb 10, 2011 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.