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)
- 前言
- object_getIvar
- object_getClass
- object_getClassName
- object_setClass
- object_isClass
- object_setIvar
- objc_getClass
- Too many arguments to function call...
- objc_msgSend
- class_copyIvarList和class_copyMethodList
- method_exchangeImplementations
- 运用SEL,运行时改变两个方法的实现
- class_getInstanceMethod和class_getClassMethod
- class_respondsToSelector
- Objective-C Runtime 运行时之三:方法与消息