向OC类中添加默认的协议实现(ProtocolKit)
以forkingdog的PorotocolKit举例
举例
ProtocolKit Protocol extension for Objective-C Usage Your protocol: @protocol Forkable <NSObject> @optional
- (void)fork; @required
- (NSString *)github; @end
Protocol extension, add default implementation, use @defs magic keyword @defs(Forkable) - (void)fork {
NSLog(@"Forkable protocol extension: I'm forking (%@).", self.github);
} - (NSString *)github {
return @"This is a required method, concrete class must override me.";
} @end
Your concrete class @interface Forkingdog : NSObject <Forkable>
@end @implementation Forkingdog - (NSString *)github {
return @"https://github.com/forkingdog";
} @end
Run test [[Forkingdog new] fork];
Result [Console] Forkable protocol extension: I'm forking (https://github.com/forkingdog).
实现
我们可以看到关键字是@def 查看其定义是
// For a magic reserved keyword color, use @defs(your_protocol_name)
#define defs _pk_extension // Interface
#define _pk_extension($protocol) _pk_extension_imp($protocol, _pk_get_container_class($protocol)) // Implementation
#define _pk_extension_imp($protocol, $container_class) \
protocol $protocol; \
@interface $container_class : NSObject <$protocol> @end \
@implementation $container_class \
+ (void)load { \
_pk_extension_load(@protocol($protocol), $container_class.class); \
} \ // Get container class name by counter
#define _pk_get_container_class($protocol) _pk_get_container_class_imp($protocol, __COUNTER__)
#define _pk_get_container_class_imp($protocol, $counter) _pk_get_container_class_imp_concat(__PKContainer_, $protocol, $counter)
#define _pk_get_container_class_imp_concat($a, $b, $c) $a ## $b ## _ ## $c
转化为oc语言定义就是
@protocol Forkable; \
@interface __PKContainer_Forkable_0 : NSObject <Forkable> @end \
@implementation __PKContainer_Forkable_0 \
@synthesize title = _title; + (void)load { \
//add protocol class method?
_pk_extension_load(@protocol(Forkable), __PKContainer_Forkable_0.class);
} -(NSString *)title
{
if (!_title) {
_title = @"default title!";
}
return _title;
} - (void)fork {
NSLog(@"Forkable protocol extension: I'm forking (%@).", self.github);
} - (NSString *)github {
return @"This is a required method, concrete class must override me.";
}
@end
从这可以看出关键的代码 _pk_extension_load()
可以猜想其定义是
1 在load里注册自己包含那些扩展协议
2 在c方法里遍历所有的类 把有扩展的 方法的实现指过去
现在查看其函数实现 关键代码如下
__attribute__((constructor)) static void _pk_extension_inject_entry(void) {
pthread_mutex_lock(&protocolsLoadingLock);
unsigned classCount = ;
Class *allClasses = objc_copyClassList(&classCount);
@autoreleasepool {
for (unsigned protocolIndex = ; protocolIndex < extendedProtcolCount; ++protocolIndex) {
PKExtendedProtocol extendedProtcol = allExtendedProtocols[protocolIndex];
for (unsigned classIndex = ; classIndex < classCount; ++classIndex) {
Class class = allClasses[classIndex];
if (!class_conformsToProtocol(class, extendedProtcol.protocol)) {
continue;
}
_pk_extension_inject_class(class, extendedProtcol);
}
}
}
pthread_mutex_unlock(&protocolsLoadingLock);
free(allClasses);
free(allExtendedProtocols);
extendedProtcolCount = , extendedProtcolCapacity = ;
}
//将协议的方法添加到到类中(如果不存在就不添加了)//包括类方法和实例方法
static void _pk_extension_inject_class(Class targetClass, PKExtendedProtocol extendedProtocol) { for (unsigned methodIndex = 0; methodIndex < extendedProtocol.instanceMethodCount; ++methodIndex) {
Method method = extendedProtocol.instanceMethods[methodIndex];
SEL selector = method_getName(method); if (class_getInstanceMethod(targetClass, selector)) {
continue;
} IMP imp = method_getImplementation(method);
const char *types = method_getTypeEncoding(method);
class_addMethod(targetClass, selector, imp, types);
} Class targetMetaClass = object_getClass(targetClass);
for (unsigned methodIndex = 0; methodIndex < extendedProtocol.classMethodCount; ++methodIndex) {
Method method = extendedProtocol.classMethods[methodIndex];
SEL selector = method_getName(method); if (selector == @selector(load) || selector == @selector(initialize)) {
continue;
}
if (class_getInstanceMethod(targetMetaClass, selector)) {
continue;
}
IMP imp = method_getImplementation(method);
const char *types = method_getTypeEncoding(method);
class_addMethod(targetMetaClass, selector, imp, types);
}
}
由此可知猜想是正确的,方法告诉我们对于某一个含有该协议的类,如果其含有了协议里面的函数,则跳过,如果没有该函数就添加。
这里是下载地址
向OC类中添加默认的协议实现(ProtocolKit)的更多相关文章
- 在SpringMVC中,当Json序列化,反序列化失败的时候,会抛出HttpMessageNotReadableException异常, 当Bean validation失败的时候,会抛出MethodArgumentNotValidException异常,因此,只需要在ExceptionHandler类中添加处理对应异常的方法即可。
在SpringMVC中,当Json序列化,反序列化失败的时候,会抛出HttpMessageNotReadableException异常, 当Bean validation失败的时候,会抛出Method ...
- AE 向已存在的要素类中添加字段
风过无痕 原文向已存在的要素类中添加字段 以前,在用AE写程序的时候,为了方便,一般都是直接新建一个MapControl窗体应用程序.这次需要解决的问题用不到窗口,就突发奇想,直接新建了一个Conso ...
- C++中若类中没有默认构造函数,如何使用对象数组
前言: 如果定义一个类,有其默认的构造函数,则使用new动态实例化一个对象数组,不是件难事,如下代码: #include <memory> #include <iostream> ...
- 类中添加log4j日志
在编写代码的时候需要随时查看工作日志,查看工作日志的好处就是随时能检查出错误.所以我一般就需要在编写代码的前期添加工作日志,以便更好的查看相关错误输出. 以一个springmvc小demo为例子 主 ...
- 【2016.3.30项目技术记录】]VS2010自动生成MFC单文档框架程序的修改:去除属性框,在CViewTree类中添加鼠标单击响应
转自http://blog.csdn.net/yanfeiouc2009/archive/2010/06/07/5653360.aspx 手头上有个东西要用到单文档,由于想省事,直接用VS2010做了 ...
- springboot在工具类中添加service的方法,显示为空的解决方案
@Component// 1.将工具类声明为spring组件,这个必须不能忘 public class TestUtils { //2.自动注入 @Autowired private ItemServ ...
- C++空类中的默认函数
定义一个空的C++类,例如 class Empty { } 一个空的class在C++编译器处理过后就不再为空,编译器会自动地为我们声明一些member function,一般编译过去就相当于 cla ...
- 【c++】类中带默认参数的函数
反思两个问题 1. 带默认参数的函数,为何声明.定义不能同时有参数? 2. 带默认参数的函数, 为何带默认参数的参数靠后站? 上程序 #include <iostream> #includ ...
- 给VS类文件添加默认头注释
找到类文件所在路径:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSharp\ ...
随机推荐
- Project interpreter not specified(eclipse+pydev)
[小记] 近期由于想配置Android的开发环境,把原来的MyEclipse5.5删了,下载了最新的Eclipse3.7版本号,由于之前在进行Python开发,就下载了最新的Pydev2.4版本号,安 ...
- [React ] React Fundamentals: Component Lifecycle - Mounting Usage
The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson ...
- Linux内核:关于中断你须要知道的
1.中断处理程序与其它内核函数真正的差别在于,中断处理程序是被内核调用来对应中断的,而它们执行于中断上下文(原子上下文)中,在该上下文中执行的代码不可堵塞. 中断就是由硬件打断操作系统. 2.异常与中 ...
- android 换肤模式总结
由于Android的设置中并没有夜间模式的选项,对于喜欢睡前玩手机的用户,只能简单的调节手机屏幕亮度来改善体验.目前越来越多的应用开始把夜间模式加到自家应用中,没准不久google也会把这项功能添加到 ...
- 监控Linux内存使用情况
cat mem.sh#!/bin/bashIP=`ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk -F ' ' '{print $2}'| aw ...
- [Form builder]:about SYSTEM.MESSAGE_LEVEL
If you want to suppress error messages then you have to set a system variable :system.message_level. ...
- 查看kindle paperwhite2上卡索引书籍的方法
昨天kindle耗电量突然加快,经过检查和网络搜索得知是卡索引导致的耗电量增大.我自己通过关闭索引的方式解决了这个问题. 在这个过程中发现了一个可以直接找到所有卡索引书籍的方法,在此分享一下. 首先打 ...
- modelsim将波形文件保存下来却不打开图形界面
vsim -c -do sim.tcl -l sim.log 波形怎么保存啊,我想把波形保存下来,但不显示,想看那个信号时在加上,不然信号全部出来了太乱了 在do文件里写add wave -r *ru ...
- 【转】SharePoint 中实现ReportView
微软的Visual studio提供了ReportViewer控件以及RDLC报表设计工具.下文主要介绍如何在Sharepoint 2010项目开发中使用ReportViewer和RDLC生成项目报表 ...
- 关于sqlserver 2008 远程导入表数据
/*不同服务器数据库之间的数据操作*/ --创建链接服务器 exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' ex ...