Swift静态和动态分发机制

Free functions, and methods called on structs, are statically dispatched. This means the function that’ll be called is known at compile time. This also means the compiler might be able to inline the function, i.e. not call the function at all, but instead replace it with the code the function would execute. It can also discard or simplify code that it can prove at compile time won’t actually run.

全局函数和或者在结构上调用的方法使用的是静态分发的机制。这意味着函数调用在编译时已知。这同样表示编译器内联函数。举个例子,不是调用函数而是将其替换为函数本身的代码。他同样可以丢弃和简化编译时被证明实际不运行的代码。

Methods on classes or protocols might be dynamically dispatched. This means the compiler doesn’t necessarily know at compile time which function will run. This dynamic behavior is done either by using vtables (similar to how Java or C++ dynamic dispatch works), or in the case of @objc classes and protocols, by using selectors and objc_msgSend.

在类或协议上的方法是动态分发的。这意味着编译器并不一定知道在编译时刻哪个函数将会运行。这种动态行为用虚表(类似于Java或C++动态分发那样)或者在使用@objc修饰类或协议的情况下,使用选择器和objc_msgSend来完成。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值