1.消息机制
#import "ViewController.h"
#import "Person.h"
// 使用运行时的第一步:导入
// 第二步:Build Setting -> 搜索msg -> 设置属性为No
#import
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Person *p = [[Person alloc] init];
// 吃东西
// [p eat];
// OC:运行时机制,消息机制是运行时机制最重要的机制
// 消息机制:任何方法调用,本质都是发送消息
// SEL:方法编号,根据方法编号就可以找到对应方法实现
// [p performSelector:@selector(eat)];
// 运行时,发送消息,谁做事情就那谁
// xcode5之后,苹果不建议使用底层方法
// xcode5之后,使用运行时.
// 让p发送消息
objc_msgSend(p, @selector(eat));
objc_msgSend(p, @selector(run:),10);
// 类名调用类方法,本质类名转换成类对象
// [Person eat];
// 获取类对象
Class personClass = [Person class];
// [personClass performSelector:@selector(eat)];
// 运行时
objc_msgSend(personClass, @selector(eat));
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
2.交换方法
#import "ViewController.h"
//#import "UIImage+Image.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// UIImage *image = [UIImage imageNamed:@"123"];
// 1.每次使用,都需要导入头文件
// 2.当一个项目开发太久,使用这个方式不靠谱
[UIImage imageNamed:@"123"];
// imageNamed:
// 实现方法:底层调用xmg_imageNamed
// 本质:交换两个方法的实现imageNamed和xmg_imageNamed方法
// 调用imageNamed其实就是调用xmg_imageNamed
// imageNamed加载图片,并不知道图片是否加载成功
// 以后调用imageNamed的时候,就知道图片是否加载
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "UIImage+Image.h"
#import
@implementation UIImage (Image)
// 加载这个分类的时候调用
+ (void)load
{
// 交换方法实现,方法都是定义在类里面
// class_getMethodImplementation:获取方法实现
// class_getInstanceMethod:获取对象
// class_getClassMethod:获取类方法
// IMP:方法实现
// imageNamed
// Class:获取哪个类方法
// SEL:获取方法编号,根据SEL就能去对应的类找方法
Method imageNameMethod = class_getClassMethod([UIImage class], @selector(imageNamed:));
// xmg_imageNamed
Method xmg_imageNamedMethod = class_getClassMethod([UIImage class], @selector(xmg_imageNamed:));
// 交换方法实现
method_exchangeImplementations(imageNameMethod, xmg_imageNamedMethod);
}
// 运行时
// 先写一个其他方法,实现这个功能
// 在分类里面不能调用super,分类木有父类
//+ (UIImage *)imageNamed:(NSString *)name
//{
// [super im]
//}
+ (UIImage *)xmg_imageNamed:(NSString *)imageName
{
// 1.加载图片
UIImage *image = [UIImage xmg_imageNamed:imageName];
// 2.判断功能
if (image == nil) {
NSLog(@"加载image为空");
}
return image;
}
@end
3.动态添加方法
#import
@interface Person : NSObject
@end
#import "Person.h"
#import
@implementation Person
// 定义函数
// 没有返回值,参数(id,SEL)
// void(id,SEL)
void aaaa(id self, SEL _cmd, id param1)
{
NSLog(@"调用eat %@ %@ %@",self,NSStringFromSelector(_cmd),param1);
}
// 默认一个方法都有两个参数,self,_cmd,隐式参数
// self:方法调用者
// _cmd:调用方法的编号
// 动态添加方法,首先实现这个resolveInstanceMethod
// resolveInstanceMethod调用:当调用了没有实现的方法没有实现就会调用resolveInstanceMethod
// resolveInstanceMethod作用:就知道哪些方法没有实现,从而动态添加方法
// sel:没有实现方法
+ (BOOL)resolveInstanceMethod:(SEL)sel
{
// NSLog(@"%@",NSStringFromSelector(sel));
// 动态添加eat方法
if (sel == @selector(eat:)) {
/*
cls:给哪个类添加方法
SEL:添加方法的方法编号是什么
IMP:方法实现,函数入口,函数名
types:方法类型
*/
// @:对象 :SEL
class_addMethod(self, sel, (IMP)aaaa, "v@:@");
// 处理完
return YES;
}
return [super resolveInstanceMethod:sel];
}
@end
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// performSelector:动态添加方法
Person *p = [[Person alloc] init];
// 动态添加方法
// [p performSelector:@selector(eat)];
[p performSelector:@selector(eat:) withObject:@111];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
4.分类添加属性
#import
@interface NSObject (Objc)
@property (nonatomic, strong) NSString *name;
@end
#import "NSObject+Objc.h"
#import
@implementation NSObject (Objc)
//static NSString *_name;
- (void)setName:(NSString *)name
{
// 添加属性,跟对象
// 给某个对象产生关联,添加属性
// object:给哪个对象添加属性
// key:属性名,根据key去获取关联的对象 ,void * == id
// value:关联的值
// policy:策越
objc_setAssociatedObject(self, @"name", name, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
// _name = name;
}
- (NSString *)name
{
return objc_getAssociatedObject(self, @"name");
}
@end
#import "ViewController.h"
#import "NSObject+Objc.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSObject *objc = [[NSObject alloc] init];
objc.name = @"123";
NSLog(@"%@",objc.name);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
5.字典转模型KVC实现
#import
@interface Status : NSObject
// 写一段程序自动生成属性代码
@property (nonatomic, assign) NSInteger ID;
// 解析字典自动生成属性代码
@property (nonatomic, strong) NSString *source;
@property (nonatomic, assign) int reposts_count;
@property (nonatomic, strong) NSArray *pic_urls;
@property (nonatomic, strong) NSString *created_at;
@property (nonatomic, assign) int attitudes_count;
@property (nonatomic, strong) NSString *idstr;
@property (nonatomic, strong) NSString *text;
@property (nonatomic, assign) int comments_count;
@property (nonatomic, strong) NSDictionary *user;
// 模型的属性名跟字典一一对应
+ (__kindof Status *)statusWithDict:(NSDictionary *)dict;
@end
#import "Status.h"
@implementation Status
+ (Status *)statusWithDict:(NSDictionary *)dict
{
Status *status = [[self alloc] init];
// KVC
[status setValuesForKeysWithDictionary:dict];
return status;
}
// 解决KVC报错
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
if ([key isEqualToString:@"id"]) {
_ID = [value integerValue];
}
// key:没有找到key
// value:没有找到key对应的值
NSLog(@"%@ %@",key,value);
}
@end
#import
@interface NSObject (Property)
+ (void)createPropertyCodeWithDict:(NSDictionary *)dict;
@end
#import "NSObject+Property.h"
@implementation NSObject (Property)
+ (void)createPropertyCodeWithDict:(NSDictionary *)dict
{
NSMutableString *strM = [NSMutableString string];
// 遍历字典
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull propertyName, id _Nonnull value, BOOL * _Nonnull stop) {
// NSLog(@"%@ %@",propertyName,[value class]);
NSString *code;
if ([value isKindOfClass:NSClassFromString(@"__NSCFString")]) {
code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@;",propertyName]
;
}else if ([value isKindOfClass:NSClassFromString(@"__NSCFNumber")]){
code = [NSString stringWithFormat:@"@property (nonatomic, assign) int %@;",propertyName]
;
}else if ([value isKindOfClass:NSClassFromString(@"__NSCFArray")]){
code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;",propertyName]
;
}else if ([value isKindOfClass:NSClassFromString(@"__NSCFDictionary")]){
code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;",propertyName]
;
}else if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
code = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",propertyName]
;
}
[strM appendFormat:@"\n%@\n",code];
}];
NSLog(@"%@",strM);
}
@end
#import "ViewController.h"
#import "NSObject+Property.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 解析Plist
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"status.plist" ofType:nil];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSArray *dictArr = dict[@"statuses"];
// 设计模型属性代码
[NSObject createPropertyCodeWithDict:dictArr[0][@"user"]];
for (NSDictionary *dict in dictArr) {
// 字典转模型
}
NSMutableArray *statuses = [NSMutableArray array];
for (NSDictionary *dict in dictArr) {
// 字典转模型
Status *status = [Status statusWithDict:dict];
[statuses addObject:status];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
6.运行时字典转模型
#import
@interface User : NSObject
@property (nonatomic, strong) NSString *profile_image_url;
@property (nonatomic, assign) BOOL vip;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) int mbrank;
@property (nonatomic, assign) int mbtype;
@end
#import "User.h"
@implementation User
@end
#import
@class User;
@interface Status : NSObject
// 写一段程序自动生成属性代码
@property (nonatomic, assign) NSInteger ID;
// 解析字典自动生成属性代码
@property (nonatomic, strong) NSString *source;
@property (nonatomic, assign) NSInteger reposts_count;
@property (nonatomic, strong) NSArray *pic_urls;
@property (nonatomic, strong) NSString *created_at;
@property (nonatomic, assign) int attitudes_count;
@property (nonatomic, strong) NSString *idstr;
@property (nonatomic, strong) NSString *text;
@property (nonatomic, assign) int comments_count;
@property (nonatomic, strong) User *user;
@property (nonatomic, strong) NSDictionary *retweeted_status;
// 模型的属性名跟字典一一对应
+ (__kindof Status *)statusWithDict:(NSDictionary *)dict;
@end
#import "Status.h"
@implementation Status
+ (Status *)statusWithDict:(NSDictionary *)dict
{
Status *status = [[self alloc] init];
// KVC
[status setValuesForKeysWithDictionary:dict];
return status;
}
// 解决KVC报错
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
if ([key isEqualToString:@"id"]) {
_ID = [value integerValue];
}
// key:没有找到key
// value:没有找到key对应的值
NSLog(@"%@ %@",key,value);
}
@end
#import
@interface NSObject (Model)
+ (instancetype)modelWithDict:(NSDictionary *)dict;
@end
#import "NSObject+Model.h"
#import
/*
Ivar ivar1;
Ivar ivar2;
Ivar ivar3;
Ivar a[] = {ivar3,ivar1,ivar2};
Ivar *ivar = &a;
*/
@implementation NSObject (Model)
+ (instancetype)modelWithDict:(NSDictionary *)dict{
// 1.创建对应类的对象
id objc = [[self alloc] init];
// runtime:遍历模型中所有成员属性,去字典中查找
// 属性定义在哪,定义在类,类里面有个属性列表(数组)
// 遍历模型所有成员属性
// ivar:成员属性
// class_copyIvarList:把成员属性列表复制一份给你
// Ivar *:指向Ivar指针
// Ivar *:指向一个成员变量数组
// class:获取哪个类的成员属性列表
// count:成员属性总数
unsigned int count = 0;
Ivar *ivarList = class_copyIvarList(self, &count);
for (int i = 0 ; i < count; i++) {
// 获取成员属性
Ivar ivar = ivarList[i];
// 获取成员名
NSString *propertyName = [NSString stringWithUTF8String:ivar_getName(ivar)];
;
// 获取key
NSString *key = [propertyName substringFromIndex:1];
// user value:字典
// 获取字典的value
id value = dict[key];
// 给模型的属性赋值
// value:字典的值
// key:属性名
// 成员属性类型
NSString *propertyType = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
// user:NSDictionary
// 二级转换
// 值是字典,成员属性的类型不是字典,才需要转换成模型
if ([value isKindOfClass:[NSDictionary class]] && ![propertyType containsString:@"NS"]) { // 需要字典转换成模型
// 转换成哪个类型
// @"@\"User\"" User
NSRange range = [propertyType rangeOfString:@"\""];
propertyType = [propertyType substringFromIndex:range.location + range.length];
// User\"";
range = [propertyType rangeOfString:@"\""];
propertyType = [propertyType substringToIndex:range.location];
// 字符串截取
// 获取需要转换类的类对象
Class modelClass = NSClassFromString(propertyType);
if (modelClass) {
value = [modelClass modelWithDict:value];
}
}
if (value) {
// KVC赋值:不能传空
[objc setValue:value forKey:key];
}
}
return objc;
}
@end
#import "ViewController.h"
#import "Status.h"
#import "NSObject+Model.h"
@interface ViewController ()
@end
@implementation ViewController
/*
KVC:遍历字典中所有key,去模型中查找有没有对应的属性名
runtime:遍历模型中所有属性名,去字典中查找
*/
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 解析Plist
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"status.plist" ofType:nil];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSArray *dictArr = dict[@"statuses"];
NSMutableArray *statuses = [NSMutableArray array];
// 遍历字典数组
for (NSDictionary *dict in dictArr) {
Status *status = [Status modelWithDict:dict];
[statuses addObject:status];
}
NSLog(@"%@",statuses);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end