@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?

最新文章

  1. sqlalchemy默认时间
  2. python操作mongodb数据库
  3. JSON.parse
  4. 无状态的web应用
  5. 登录验证码编写(jsp+servlet+dao)
  6. 【Android 系统开发】Android JNI/NDK (三) 之 JNIEnv 解析
  7. Linux/Unix双机建立信任教程
  8. mvc 相关js
  9. Java对象引用
  10. 基数---SQL Server 2008 Bible
  11. 用户登录之cookie信息安全一二事
  12. mysql与oracle在groupby语句上的细节差异
  13. Strusts2--课程笔记9
  14. mongodb Decimal Spring data mongodb Decimal128 SpringMvc 序列化字符串 json converter
  15. JavaScript 正则表达式全面总结
  16. Gartner 2018 数据库系列报告发布 巨杉数据库连续两年入选
  17. hive学习03-求一年中的最大温度
  18. yum与rpm的区别以及详细介绍
  19. ES6知识总结
  20. 4.IIC总线

热门文章

  1. node-npm/yarn
  2. java 代理模式具体解释
  3. MySQL-数据库创建与删除
  4. 第一个GraphX程序
  5. 前端为啥要用javascript框架
  6. Semaphore and SemaphoreSlim
  7. YTU 2516: 剪刀石头布
  8. 【转载】limits.h
  9. [NOI2004]小H的小屋 贪心
  10. CAS和Oauth2的区别