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

Tun*_*her -7 objective-c

这段代码意味着什么?

[images isKindOfClass:[NSArray class]]
Run Code Online (Sandbox Code Playgroud)

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

谢谢

the*_*ner 5

isKindOfClass当对象从(或是)给定类继承时返回true.在这种情况下,它检查是否images是a NSArray的子类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];
}
Run Code Online (Sandbox Code Playgroud)

  • 检查还会看到它是否是同一个类,而不仅仅是继承. (2认同)