#import <Foundation/Foundation.h>
@interface Dog : NSObject
- (void)bark;
@end
#import "Dog.h"
@implementation Dog
- (void)bark{
NSLog(@"wonwonwonwogh..............");
}
@end
#import <Foundation/Foundation.h>
#import "Dog.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Dog *dog1=[[Dog alloc]init];
Dog *dog2=[[Dog alloc]init];
Dog *dog3=[[Dog alloc]init];
NSArray *arr=[[NSArray alloc]initWithObjects:@"one",@"two",dog1,@"oc",dog2,dog3,nil];
for (id obj in arr) {
// [Dog class]获取一个类 的类型 Class类型
//Class 存放的是类的信息
//- isKindOfClass:判断一个对象是否是某个类的实例
if ([obj isKindOfClass:[Dog class]]) {
//判断obj指向的对象 是否是 Dog 类的实例
[obj bark];
}else{
NSLog(@"I am a string");
}
}
}
return 0;
}
转载于:https://my.oschina.net/u/2410306/blog/519818