6/17/2009

if([myObject isKindOfClass:[UIView class]])

This gets pretty handy, the isKindOfClass: method. It's self explanatory, it gives you a boolean of whether if it is a certain class or not. I usually use it when I have random classes in an array and wants to do something with a specific class.

けっこうよく使うメソッドがこれ、isKindOfClass。配列にいろんなクラスをいれていて例えばUIViewのクラスならば、というふうな命令が下せる。


NSMutableArray *anArray = [[NSMutableArray alloc] init];
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
[anArray addObject: [NSNumber numberWithInt:284]];
[anArray addObject: aView];
[anArray addObject: [NSString stringWithFormat:@"a string"]];

for (id aObject in anArray){
if([aObject isKindOfClass:[UIView class]]){
NSLog(@"aObject is a UIView: %@", aObject);
}
}

No comments:

Post a Comment