runtime调用方法
- (void)test { TestModel *objct = [[TestModel alloc] init];
((void (*) (id, SEL)) objc_msgSend) (objct, sel_registerName("country"));
((void (*) (id, SEL, NSString *)) objc_msgSend) (objct, sel_registerName("getProvince:"), @"广东省");
((void (*) (id, SEL, NSString *, NSString *)) objc_msgSend) (objct, sel_registerName("getCity:station:"), @"深圳市", @"世界之窗");
NSString *weather = ((NSString* (*) (id, SEL)) objc_msgSend) (objct, sel_registerName("getWeather"));
NSLog(@"%@", weather);
}给分类添加属性
#import <objc/runtime.h> static void *TestStringKey = &TestStringKey;
@implementation TestModel (String)
- (void)setTestString:(NSString *)testString {
objc_setAssociatedObject(self, TestStringKey, testString, OBJC_ASSOCIATION_COPY);
}
- (NSString *)testString {
return objc_getAssociatedObject(self, TestStringKey);
}
@end获取属性
使用class_copyIvarList获得的属性数组是Ivar类型 可以使用ivar_getName将ivar类型转换为char
再使用NSString 的stringWithUTF8String方法转化为NSString类型。
Runtime运行时
Copyright © 2015 Powered by MWeb, Theme used GitHub CSS.