开发者

Objective-C: How to check if a class gets extended by a category?

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! No开发者_StackOverflow社区w, 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)]) {


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;


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜