一、在项目中新建简单一个Robot类,该类只有name、uid属性,以及一个简单- (void)printRobotData 的方法:
Robot.h文件中代码:
image.png
//
// Robot.h
// RuntimeDemo
//
// Created by GrabinWong on 2017/12/12.
// Copyright © 2017年 GrabinWong. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Robot : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *uid;
- (void)printRobotData;
@end
Robot.m文件中代码:
image.png
//
// Robot.m
// RuntimeDemo
//
// Created by GrabinWong on 2017/12/12.
// Copyright © 2017年 GrabinWong. All rights reserved.
//
#import "Robot.h"
@implementation Robot
- (void)printRobotData
{
NSLog(@"This robit named %@,and it's id is %@.",self.name,self.uid);
}
@end
二、接下来就是为Robot类创建Category和Extentsion来对比它们之间的区别了。
image.png
image.png
创建完可以发现,Category有.h和.m两个文件,而Extentsion只有.h文件。
image.png
A、尝试在Extentsion中添加属性:master、price和方法- (void)sayHello;
image.png
//
// Robot+RobotExtentsion.h
// RuntimeDemo
//
// Created by GrabinWong on 2017/12/12.
// Copyright © 2017年 GrabinWong. All rights reserved.
//
#import "Robot.h"
@interface Robot ()
@property (nonatomic, copy) NSString *master;
@property (nonatomic, copy) NSString *price;
- (void)sayHello;
@end
实例化属性和方法,运行会发现,添加属性是没问题的,但添加方法因为Extentsion只有.h文件。所以方法只有声明,没有方法实现,所以崩了:
image.png
解决方法是,在原有的类Robot中添加方法实现:
image.png
总结一下:类的Extentsion(延展)只有.h文件,它可以为类添加属性,不需要去实现setter,getter方法,可以去为类添加方法,但方法的实现是得在原有类的.m文件中实现。
B、尝试在Category中添加属性:user和方法- (void)printUser;
Robot+RobotCategory.h文件中代码:
image.png
Robot+RobotCategory.m文件中代码:
image.png
运行崩了,原因是属性user没有setter方法,👆上面的图其实也有给了警告。
image.png
解决方法是,在Robot+RobotCategory.m中用runtime实现属性的setter、getter方法实现,这也是runtime其中的用法,可以为category添加属性:
image.png
//
// Robot+RobotCategory.m
// RuntimeDemo
//
// Created by GrabinWong on 2017/12/12.
// Copyright © 2017年 GrabinWong. All rights reserved.
//
#import "Robot+RobotCategory.h"
#import <objc/runtime.h>
static const char *kUserKey = "kUserKey";
@implementation Robot (RobotCategory)
- (void)setUser:(NSString *)user
{
objc_setAssociatedObject(self, kUserKey, user, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)user
{
return objc_getAssociatedObject(self, kUserKey);
}
- (void)printUser
{
NSLog(@"Robot's user is %@",self.user);
}
@end
总结一下:类的Category(类别)有.h和.m文件,它可以为类添加方法,可以为类添加属性,但得用runtime为属性添加setter、getter方法。
Runtime的基本使用:
1.可以为类添加属性,
2.方法交换。
方法交换 method_exchangeImplementations
在分类中添加一个方法,用于测试方法交换
image.png
//
// Robot+RobotCategory.m
// RuntimeDemo
//
// Created by GrabinWong on 2017/12/12.
// Copyright © 2017年 GrabinWong. All rights reserved.
//
#import "Robot+RobotCategory.h"
#import <objc/runtime.h>
static const char *kUserKey = "kUserKey";
@implementation Robot (RobotCategory)
- (void)setUser:(NSString *)user
{
objc_setAssociatedObject(self, kUserKey, user, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)user
{
return objc_getAssociatedObject(self, kUserKey);
}
- (void)printUser
{
NSLog(@"Robot's user is %@",self.user);
}
- (void)printHelloWorld
{
NSLog(@"这是用来测试runtime方法交换的");
NSLog(@"HelloWorld");
}
+ (void)load
{
Method printRobotDataMethod = class_getInstanceMethod([self class], @selector(printUser));
Method sayHelloMethod = class_getClassMethod([self class], @selector(printHelloWorld));
method_exchangeImplementations(printRobotDataMethod, sayHelloMethod);
}
@end
class_getInstanceMethod 得到类的实例方法
class_getClassMethod 得到类方法
再用method_exchangeImplementations 进行方法交换。
可以看到结果,调用[curRobot printUser] 得到的是 printHelloWorld 方法执行的内容。
image.png
注意:把runtime方法交换的操作放在+(void)load里面是为了避免多次调用
+ (void)load
{
Method printRobotDataMethod = class_getInstanceMethod([self class], @selector(printUser));
Method sayHelloMethod = class_getClassMethod([self class], @selector(printHelloWorld));
method_exchangeImplementations(printRobotDataMethod, sayHelloMethod);
}