2015-10-07 97 views
1

我正在使用下面的代码来检查视图控制器。比较类是给出不正确的输出

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"); 
} 

我得到的输出如下。

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

you go it wrong 

任何想法是什么问题?


只是为了更新,下面就是我定义我的视图控制器...

@interface CategoriesViewController : GlobalViewController { 

现在GlobalViewController我有方法,其中,我上面的检查......

回答

1

您想要进行类检查的变量应作为对象传入,而不是作为类传入。

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

那是错误的比较。您可以在该课程的对象上致电isKindOfClass:。就像这样:

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

在你的情况下,你可能想要检查self