NSObject和反射2。
commend +R run
id stu=[Student student]; // –> Student *stu=[Student student];
:也是方法名的一部分
主要是反射:reflect 根据字符串来实例化一个对象
//#pragma mark reflect according to class name
void reflect(){
NSString *str=@”Person”;
Class class=NSClassFromString(str);//convert a string to a class
Person *person=[[class alloc]init];
NSLog(@”%@”,person);
[person release];
NSString *str=NSStringFromClass([Person class]);//obtain class name
}
//#pragma mark reflect method
Person.h
@interface Person:NSObject
-(void)test;
@end
Person.m
@implementation
-(void)test{
NSLog(@”Call test method in Person.”);
}
@end
void reflectmethod(){
NSString *method=@”test”;
//Convert this string to a method
//通过字符串调用方法名
SEL selector=NSSelectorFromString(method);
[person performSelector:(selector)];
[person release];
// Convert a(SEL) selector to a string
NSString *selectorName=NSStringFromSelector(selector);
}