@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的更多相关文章

  1. @OBJC 和 DYNAMIC

    原文转载自:@OBJC 和 DYNAMIC 虽然说 Swift 语言的初衷是希望能摆脱 Objective-C 的沉重的历史包袱和约束,但是不可否认的是经过了二十多年的洗礼,Cocoa 框架早就烙上了 ...

  2. Swift与Objective-C的兼容“黑魔法”:@objc和Dynamic

    Cocoa框架早已烙上了不可磨灭的OC印记,而无数的第三方库都是用OC写成的,这些积累无论是谁都不能小觑.苹果采取了允许开发者在同一个项目中同时使用Swift和OC进行开发的做法,但要想实现互通,又需 ...

  3. swift @objc dynamic

    @objc vs @objc dynamic @objc:  Objective-C entry points One can explicitly write @objc on any Swift ...

  4. Swift和OC混编时, 关于@objc的作用

    Objective-C 和 Swift 在底层使用的是两套完全不同的机制,Cocoa 中的 Objective-C 对象是基于运行时的,它从骨子里遵循了 KVC (Key-Value Coding,通 ...

  5. swift的@objc总结

    One can explicitly write @objc on any Swift declaration that can be expressed in Objective-C. @objc相 ...

  6. ObjC之RunTime(上)

    转载自这里. 最近看了一本书——iOS6 programming Pushing the Limits(亚马逊有中文版),最后一章是关于Deep ObjC的,主要内容是ObjC的runtime.虽然之 ...

  7. iOS ---Swift学习与复习

    swift中文网 http://www.swiftv.cn http://swifter.tips/ http://objccn.io/ http://www.swiftmi.com/code4swi ...

  8. swifter技巧(100)

    一.swift新元素 Tip1:柯里化 将方法进行柯里化,把接受多个参数的方法变换成接受第一个参数的方法,并且返回接受余下的参数,返回结果的新方法. func addTwoNumbers(a: Int ...

  9. [swift] NSClassFromString 无法获得该类

    在写OC的时候需要用 NSClassFromString(classStringName)获得一个类,如果存在就用这个类型来声明一个对象, 但是在swift的时候却往往得不到这个类,为什么呢? 从截图 ...

随机推荐

  1. git fetch 和 git pull 的差别

    Git中从远程的分支获取最新的版本号到本地有这样2个命令: 1. git fetch:相当于是从远程获取最新版本号到本地,不会自己主动merge git fetch origin master git ...

  2. (工具类)Linux笔记之终端日志记录工具script

    在学习Linux时,有时候终端的打印消息对于我们很重要,可是终端显示也是有一定的缓冲空间的.当信息打印许多时,前面的信息就会被覆盖掉.所以这里网上搜索了一下这方面的介绍.现总结例如以下: script ...

  3. C++中使用Json的方法

    使用 C++ 处理 JSON 数据交换格式 一.摘要 JSON 的全称为:JavaScript Object Notation,顾名思义,JSON 是用于标记 Javascript 对象的,JSON官 ...

  4. 新IOS编程语言 Swift 新编译器Xcode6

    https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_ ...

  5. MariaDB ----增删改

    1> 查看表的(创建)状态: show careate table students1; 注:       自增, (保护机制) 好马不吃回头草 2>查看 字段:( 查看表的结构) des ...

  6. 通过构建Cocoapods私有库进行组件化开发探索

    专题一 一.创建私有索引库 选Github或者码云都可以,本例以Github为例.创建私有索引库用来作为自己组件库的索引: 二.本地添加私有索引库 添加:pod repo add 索引库名称 索引库地 ...

  7. react新特性 react hooks

    本文介绍的是react新特性react hooks,本文面向的是有一定react开发经验的小伙伴,如果你对react还不是很熟悉的话我建议你先学习react并多多联系. 首先我们都知道react有3种 ...

  8. 运行shell脚本报错 '\357\273\277': command not found 解决的方法

    1,删除BOM,在vi以下运行以下的命令就可以 :set nobomb 2,原因: 所谓BOM,全称是Byte Order Mark.它是一个Unicode字符,通常出如今文本的开头,用来标识字节序( ...

  9. ASP.NET快速开发框架之工作流引擎

    大家好,啊!我小六六也有自己的博客了,今天我来跟大家分享下我的撸码心得,顺便吐槽下,我的坑爹上司.作为一名程序猿,我在系统开发方面也是“浸淫”了许久了!喔,不不不,是经营! 今天我就跟大家分享一下工作 ...

  10. 开始学习java编程

    先看视屏,学习JAVA语法先,后面再看java web mvc,以及myeclipse http://i.youku.com/u/UMzM4MjMxNjMy/videos 争取20天内进步很大.