💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
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); } ~~~ 输出: ![](https://box.kancloud.cn/2016-05-05_572b015446fa8.jpg)