objc_getClass

来源:互联网 发布:网络歌唱大赛 编辑:程序博客网 时间:2024/06/14 17:01

objc_getClass 以类型字符串得到类

/**  * Returns the class definition of a specified class. *  * @param name The name of the class to look up. *  * @return The Class object for the named class, or \c nil *  if the class is not registered with the Objective-C runtime. *  * @note \c objc_getClass is different from \c objc_lookUpClass in that if the class *  is not registered, \c objc_getClass calls the class handler callback and then checks *  a second time to see whether the class is registered. \c objc_lookUpClass does  *  not call the class handler callback. *  * @warning Earlier implementations of this function (prior to OS X v10.0) *  terminate the program if the class does not exist. */OBJC_EXPORT Class objc_getClass(const char *name)    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);


#import "ViewController.h"#import <objc/runtime.h>#import "Person.h"#import "Dog.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.            Class c1 = objc_getClass("Person");        Person * p1 = [[c1 alloc] init];        NSLog(@"%@", p1.name);        }

输出:


0 0