Fahim Parkar
Fahim Parkar

Reputation: 31637

Comparing class is giving incorrect output

I am using below code to check view controllers.

NSLog(@"addProductClicked 1===%@", self.class);

NSLog(@"addProductClicked 2===%@", [CategoriesViewController class]);

if ([self.class isKindOfClass:[CategoriesViewController class]]) {

    NSLog(@"you go it right");

} else {
    NSLog(@"you go it wrong");
}

The output I get is as below.

addProductClicked 1===CategoriesViewController
addProductClicked 2===CategoriesViewController

you go it wrong

Any idea what is going wrong?


Just to update, below is what I have defined my view controller...

@interface CategoriesViewController : GlobalViewController {

Now in GlobalViewController I have method where I am checking above...

Upvotes: 1

Views: 45

Answers (2)

Luke
Luke

Reputation: 11476

The variable you want to class check should be passed in as an object, not as a class.

if ([self isKindOfClass:[CategoriesViewController class]]) {
    NSLog(@"you go it right");
} else {
    NSLog(@"you go it wrong");
}

Upvotes: 1

Abhinav
Abhinav

Reputation: 38152

Thats is wrong comparison. You call isKindOfClass: on the object of that class. Something like this:

CategoriesViewController *obj = [[CategoriesViewController alloc] init];
[obj isKindOfClass:CategoriesViewController];

In your case you probably want to put a check on self.

Upvotes: 1

Related Questions