我觉得这很奇怪。所以我有一个UIView并且我想改变textcolor所有的UILabel。这是我所做的:
for (UILabel *label in [self subviews]) { // self is the UIView
label.textColor = someColor;
}
当我运行代码时,它因错误而崩溃UIImageView: unrecognized selector setTextColor: sent to instance (some instance)
所以看起来label快速枚举中的UIImageView. 顺便说一句,我确实有两个UIImageView. UIView *self但是,快速枚举不应该UILabel只给出(因为我指定UILabel *label而不是UIView *label)?
我认为这是问题所在,因为当我编写以下代码时,它可以工作。
for (UILabel *label in [self subviews]) { // self is the UIView
if ([label isKindOfClass:[UILabel class]]) {
label.textColor = someColor;
}
}
所以在这段代码中,当我检查以保证它label是 aUILabel然后设置它textcolor时,视图中的所有UILabels 都会正确地改变它们的颜色。
有人可以解释为什么我需要if-statement仔细检查实例类型吗?