我们先创建一个MyClass
类,其代码如下,用法都在代码中,有注释
MyClass.h 文件
//
// MyClass.h
// RuntimeC++
//
// Created by plee on 15/10/8.
// Copyright © 2015年 franklee. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface MyClass : NSObject<NSCopying,NSCoding>
@property (nonatomic,strong) NSArray * array;
@property (nonatomic,copy) NSString * string;
- (void)method1;
- (void)method2;
+ (void)classMethod1;
- (void)method3WithArg1:(NSInteger )arg1 arg2:(NSString * )string;
- (void)setdataWith:(NSDictionary *)dict;
@end
MyClass.m 文件如下
//
// MyClass.m
// RuntimeC++
//
// Created by plee on 15/10/8.
// Copyright © 2015年 franklee. All rights reserved.
//
#import "MyClass.h"
@interface MyClass(){
NSInteger _instance1;
NSString * _instance2;
}
@property (nonatomic,assign) NSUInteger integer;
@end
@implementation MyClass
- (void)method1{
NSLog(@"call method method1");
}
- (void)method2{
}
+ (void)classMethod1{
}
- (void)method3WithArg1:(NSInteger)arg1 arg2:(NSString *)string{
NSLog(@"arg1 : %ld,arg2 : %@",arg1,string);
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
return self;
}
- (id)copyWithZone:(NSZone *)zone{
return self;
}
- (void)setdataWith:(NSDictionary *)dict{
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if (key ) {
objc_property_t property = class_getProperty([self class], [key UTF8String]);
NSString * attriString = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
NSLog(@"%@",attriString);
[self setValue:obj forKey:key];
}
}];
}
@end
在main函数内部进行使用
//
// main.m
// RuntimeC++
//
// Created by plee on 15/10/8.
// Copyright © 2015年 franklee. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import "MyClass.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
MyClass * myCalss = [[MyClass alloc] init];
unsigned int outCount = 0;
Class cls = myCalss.class;
//类名
NSLog(@"class name : %s",class_getName(cls));
//父类
NSLog(@"super class name :%s",class_getName(class_getSuperclass(cls)));
NSLog(@"=================================");
//是否是元类
NSLog(@"MyClass is %@ a meta-class",(class_isMetaClass(cls))?@"yes":@"not");
Class meta_class = objc_getMetaClass(class_getName(cls));
NSLog(@"%s's meta-class is %s",class_getName(cls),class_getName(meta_class));
//变量实例大小
NSLog(@"instance size %zu",class_getInstanceSize(cls));
NSLog(@"------------------------------------");
//成员变量
Ivar * ivars = class_copyIvarList(cls, &outCount);
for (int i=0; i<outCount; i++) {
Ivar ivar = ivars[i];
NSLog(@"instance variable's name :%s at index : %d",ivar_getName(ivar),i);
}
free(ivars);
Ivar string = class_getInstanceVariable(cls, "_string");
if (string !=NULL) {
NSLog(@"instance variable %s",ivar_getName(string));
}
NSLog(@"-------------------------");
//属性操作
objc_property_t array = class_getProperty(cls, "array");
if (array !=NULL) {
NSLog(@"property is : %s ",property_getName(array));
}
NSLog(@"-------------------------++++++++++++");
//方法操作
Method * methods = class_copyMethodList(cls, &outCount);
for (int j=0; j<outCount; j++) {
Method method1 = methods[j];
SEL method_name = method_getName(method1);
NSString * strName = [NSString stringWithCString:sel_getName(method_name) encoding:NSUTF8StringEncoding];
NSLog(@"method's signature :%@",strName);
}
free(methods);
NSLog(@"+++++++++++++++++++++++++++");
//对象方法,减号方法
Method method1 = class_getInstanceMethod(cls, @selector(method1));
if (method1!=NULL) {
SEL method_name = method_getName(method1);
NSString * strName = [NSString stringWithCString:sel_getName(method_name) encoding:NSUTF8StringEncoding];
NSLog(@"class method :%@",strName);
}
//类方法class_getClassMethod
Method classMethod = class_getClassMethod(cls, @selector(classMethod1));
if (classMethod!=NULL) {
SEL method_name1 = method_getName(classMethod);
NSString * str = [NSString stringWithCString:sel_getName(method_name1) encoding:NSUTF8StringEncoding];
NSLog(@"class method id :%@",str);
}
NSLog(@"MyClass is %@ responced to selector :method3WithArg1:Arg2:",(class_respondsToSelector(cls, @selector(method3WithArg1:arg2:))?@"yes":@"not"));
//函数实现指针
IMP imp = class_getMethodImplementation(cls, @selector(method1));
imp();
NSLog(@"=========================");
//协议
Protocol * __unsafe_unretained * protocols = class_copyProtocolList(cls, &outCount);
Protocol * protocol;
for (int i=0; i<outCount; i++) {
protocol = protocols[i];
NSLog(@"protocol name : %s",protocol_getName(protocol));
}
NSLog(@"MyClass is %@ responsed to protocol %s ",(class_conformsToProtocol(cls, protocol)?@"yes":@"not"),protocol_getName(protocol));
int numClass ;
Class * classes = NULL;
numClass = objc_getClassList(NULL, 0);
if (numClass>0) {
classes =(Class *) malloc(sizeof(Class) * numClass);
numClass = objc_getClassList(classes, numClass);
NSLog(@"number of clesses :%d",numClass);
for (int i=0; i<numClass; i++) {
Class clsname = classes[i];
NSLog(@"class name :%s",class_getName(clsname));
}
free(classes);
}
float a[] = {1.0,2.0,3.0,4.0};
NSLog(@"array encoding type :%s",@encode(typeof(a)));
}
return 0;
}
以上都是对简单runtime函数进行的简单学习和使用,有什么新的见解都可以留言,我也只是简单的了解,未做深入的探讨