什么是“isKindOfClass”以及我们使用它的原因?

时间:2013-05-17 15:07:35

标签: objective-c

这段代码意味着什么?

[images isKindOfClass:[NSArray class]]

它返回一个布尔值,但为什么我们使用它?

由于

1 个答案:

答案 0 :(得分:5)

当对象从给定类继承(或是)时,

isKindOfClass返回true。在这种情况下,它会检查imagesNSArray还是NSArray的子类。

我正在处理的一些代码中使用的示例是检查我们正在显示的项目是否需要为iPad([ctrl isKindOfClass:[BaseSplitViewController class]])或iPhone处理。像这样:

CGRect backViewFrame = CGRectZero;
if ([currentController isKindOfClass:[BaseSplitViewController class]]) {
    //Set width and hight of background View to 1024.
    [backgroundView setFrame:CGRectMake(0, 0, 1024, 1024)];
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        backViewFrame = CGRectMake(0, 0, 1024, 768);
    } else if (UIInterfaceOrientationIsPortrait(orientation)) {
        backViewFrame = CGRectMake(0, 0, 768, 1024);
    }
} else {
    backViewFrame = currentController.view.frame;
    [backgroundView setFrame:backViewFrame];
}