由于ObjC动态性,在ObjC中实现反射可以说是相当简单,下面代码中演示了常用的反射操作,具体作用也都在代码中进行了注释说明:
Account.h
// // Account.h // FoundationFramework // // Created by Kenshin Cui on 14-2-16. // Copyright (c) 2014年 Kenshin Cui. All rights reserved. // #import <Foundation/Foundation.h> @interface Account : NSObject @property (nonatomic,assign) double balance; @end
Account.m
// // Account.m // FoundationFramework // // Created by Kenshin Cui on 14-2-16. // Copyright (c) 2014年 Kenshin Cui. All rights reserved. // #import "Account.h" @implementation Account @end
Person.h
// // Person.h // FoundationFramework // // Created by Kenshin Cui on 14-2-16. // Copyright (c) 2014年 Kenshin Cui. All rights reserved. // #import <Foundation/Foundation.h> @class Account; @interface Person : NSObject @property (nonatomic,copy) NSString *name; @property (nonatomic,retain) Account *account; -(Person *)initWithName:(NSString *)name; +(Person *)personWithName:(NSString *)name; -(void)showMessage:(NSString *)infomation; //自己实现对象比较方法 -(NSComparisonResult)comparePerson:(Person *)person; @end
Person.m
// // Person.m // FoundationFramework // // Created by Kenshin Cui on 14-2-16. // Copyright (c) 2014年 Kenshin Cui. All rights reserved. // #import "Person.h" @implementation Person -(Person *)initWithName:(NSString *)name{ if(self=[super init]){ self.name=name; } return self; } +(Person *)personWithName:(NSString *)name{ Person *person=[[Person alloc]initWithName:name]; return person; } -(void)showMessage:(NSString *)infomation{ NSLog(@"My name is %@,the infomation is \"%@\".",_name,infomation); } //自己实现对象比较方法 -(NSComparisonResult)comparePerson:(Person *)person{ return [_name compare:person.name]; } -(NSString *)description{ return [NSString stringWithFormat:@"name=%@",_name]; } @end
main.m
// // main.m // FoundationFramework // // Created by Kenshin Cui on 14-2-16. // Copyright (c) 2014年 Kenshin Cui. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { /*常用方法*/ Person *person1=[Person personWithName:@"Kenshin"]; NSLog(@"%i",[person1 isKindOfClass:[NSObject class]]); //判断一个对象是否为某种类型(如果是父类也返回YES),结果:1 NSLog(@"%i",[person1 isMemberOfClass:[NSObject class]]); //判断一个对象是否是某个类的实例化对象,结果:0 NSLog(@"%i",[person1 isMemberOfClass:[Person class]]); //结果:1 NSLog(@"%i",[person1 conformsToProtocol:@protocol(NSCopying)]);//是否实现了某个协议,结果:0 NSLog(@"%i",[person1 respondsToSelector:@selector(showMessage:)]);//是否存在某个方法,结果:1 [person1 showMessage:@"Hello,world!"];//直接调用一个方法 [person1 performSelector:@selector(showMessage:) withObject:@"Hello,world!"]; //动态调用一个方法,注意如果有参数那么参数类型只能为ObjC对象,并且最多只能有两个参数 /*反射*/ //动态生成一个类 NSString *className=@"Person"; Class myClass=NSClassFromString(className);//根据类名生成类 Person *person2=[[myClass alloc]init]; //实例化 person2.name=@"Kaoru"; NSLog(@"%@",person2);//结果:name=Kaoru //类转化为字符串 NSLog(@"%@,%@",NSStringFromClass(myClass),NSStringFromClass([Person class])); //结果:Person,Person //调用方法 NSString *methodName=@"showMessage:"; SEL mySelector=NSSelectorFromString(methodName); Person *person3=[[myClass alloc]init]; person3.name=@"Rosa"; [person3 performSelector:mySelector withObject:@"Hello,world!"]; //结果:My name is Rosa,the infomation is "Hello,world!". //方法转化为字符串 NSLog(@"%@",NSStringFromSelector(mySelector)); //结果:showMessage: return 0; }