@objc and dynamic
@objc and dynamic
Objective-C runtime visibility and the depths of dynamic dispatch in the modern Swift era.
5 December 2017 ∙ Objective-C Interop ∙ written by Greg Heo
The @objc
attribute controls visibility of Swift bits from Objective-C. It’s back under the spotlight with Swift 4 and more specifically the changes in SE-0160, Limiting @objc inference.
What does @objc
mean? What about dynamic
? How do they work? And what’s different in the post-Swift 4 world?
This article will be a bit different — we’ll compile a simple bit of code and inspect it rather than look at something like Objective-C runtime source code.
Semi-Permeable Membranes
Way back in 2015 I gave a talk titled Switching Your Brain to Swift. Swift 2 was in beta and mixed codebases were all around us.
I used the analogy of a semi-permeable membrane — things make it across the language divide from Objective-C to Swift very easily, but not as easily from Swift to Objective-C.
There are two keywords to keep in mind when dealing with interoperability:
@objc
means you want your Swift code (class, method, property, etc.) to be visible from Objective-C.dynamic
means you want to use Objective-C dynamic dispatch.
The state of the world circa 2015.
The slightly confusing bit for me was that this scheme conflates the concepts of visibility and dispatch.
In Swift 3 and earlier, dynamic
also implied @objc
. New in Swift 4, dynamic
only means dynamic dispatch and says nothing about Objective-C visibility.
However, there’s no such thing as Swift dynamic dispatch; we only have the Objective-C runtime’s dynamic dispatch. That means you can’t have just dynamic
and you must write @objc dynamic
. So this is effectively the same situation as before, just made explicit.
That’s really all there is to it. But where’s the fun in that? How does it work under the hood? Can we start to unbox @objc
and dynamic
?
@objc and dynamic的更多相关文章
- @OBJC 和 DYNAMIC
原文转载自:@OBJC 和 DYNAMIC 虽然说 Swift 语言的初衷是希望能摆脱 Objective-C 的沉重的历史包袱和约束,但是不可否认的是经过了二十多年的洗礼,Cocoa 框架早就烙上了 ...
- Swift与Objective-C的兼容“黑魔法”:@objc和Dynamic
Cocoa框架早已烙上了不可磨灭的OC印记,而无数的第三方库都是用OC写成的,这些积累无论是谁都不能小觑.苹果采取了允许开发者在同一个项目中同时使用Swift和OC进行开发的做法,但要想实现互通,又需 ...
- swift @objc dynamic
@objc vs @objc dynamic @objc: Objective-C entry points One can explicitly write @objc on any Swift ...
- Swift和OC混编时, 关于@objc的作用
Objective-C 和 Swift 在底层使用的是两套完全不同的机制,Cocoa 中的 Objective-C 对象是基于运行时的,它从骨子里遵循了 KVC (Key-Value Coding,通 ...
- swift的@objc总结
One can explicitly write @objc on any Swift declaration that can be expressed in Objective-C. @objc相 ...
- ObjC之RunTime(上)
转载自这里. 最近看了一本书——iOS6 programming Pushing the Limits(亚马逊有中文版),最后一章是关于Deep ObjC的,主要内容是ObjC的runtime.虽然之 ...
- iOS ---Swift学习与复习
swift中文网 http://www.swiftv.cn http://swifter.tips/ http://objccn.io/ http://www.swiftmi.com/code4swi ...
- swifter技巧(100)
一.swift新元素 Tip1:柯里化 将方法进行柯里化,把接受多个参数的方法变换成接受第一个参数的方法,并且返回接受余下的参数,返回结果的新方法. func addTwoNumbers(a: Int ...
- [swift] NSClassFromString 无法获得该类
在写OC的时候需要用 NSClassFromString(classStringName)获得一个类,如果存在就用这个类型来声明一个对象, 但是在swift的时候却往往得不到这个类,为什么呢? 从截图 ...
随机推荐
- Python循环定时服务功能(相似contrab)
Python实现的循环定时服务功能.类似于Linux下的contrab功能.主要通过定时器实现. 注:Python中的threading.timer是基于线程实现的.每次定时事件产生时.回调完响应函数 ...
- QML与C++交互:登陆界面设计
QML与C++交互:登陆界面设计 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN7 开发环境:Qt5.2.1 说明: QML设计前 ...
- iOS开发——高级篇——iOS 强制退出程序APP代码
1.先po代码 UIAlertView* alert = [[UIAlertView alloc] initWithTitle:self.exitapplication message:@" ...
- 【OI】对拍
对拍的方法是先用生成器生成几组随机数据,然后用暴力算法和当前算法对比结果来确保正确性. 数据生成器: 引入cstdlib与ctime两个库,然后通过srand初始化随机数: srand(time(0) ...
- .NET下为百度文本编辑器UEditor增加图片删除功能
[摘要:比来写了个项目,用到了UEditor,但是UE并出有文件删除功效 然后网上找若何增加 找半天只能找到一个1.2.X的 以是便摹仿PHP的 改成了.NET的 PHP本文 第一步 (增加背景删除地 ...
- docker映射端口与ssh访问或容器访问
映射端口 -d 后台执行 -p映射端口 --privileged 可以使用systemctl # docker run --privileged -d -p 9000:80 jiqing9006/ce ...
- 网络流之最大流算法(EK算法和Dinc算法)
最大流 网络流的定义: 在一个网络(有流量)中有两个特殊的点,一个是网络的源点(s),流量只出不进,一个是网络的汇点(t),流量只进不出. 最大流:就是求s-->t的最大流量 假设 u,v 两个 ...
- codeforces 940F 带修改的莫队
F. Machine Learning time limit per test 4 seconds memory limit per test 512 megabytes input standard ...
- JeePlus:API工具
ylbtech-JeePlus:API工具 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 0. http://wiki.jeeplus.org/docs/sho ...
- .gitignore(转载)
转自:http://blog.csdn.net/liuqiaoyu080512/article/details/8648266 git 可以管理所有文件的变更, 但并不是所有文件都有意义. 大部分二进 ...