Swift中使用inline

本文介绍了Swift中@inline()的使用,包括始终内联和禁止内联的场景。通过示例展示了如何影响函数的内联行为,指出在Swift中不需要显式使用@inline(),因为编译器会自动判断。
摘要由CSDN通过智能技术生成

Swift中使用inline

OC代码编写中我们经常会使用static inline来修饰函数替代,得到更好的性能。
那么在swift中如何使用呢?

@inline(option)

@inline(__always) func foo1() {
   
    /// do something...
}
@inline(never) func foo2() {
   
    /// do something...
}

@inline(__always) - will make sure to always inline the function. Achieve this behavior by adding @inline(__always) before the function. Use “if your function is rather small and you would prefer your app ran faster.”
@inline(never) - will make sure to never inline the function. This can be achieved by adding @inline(never) before the function. Use “if your function is quite long and you want to avoid increasing your code segment size.”

使用 @inline(__always)会确保函数总是inline

使用@inline(never)会确保函数inline

我们知道在OC中即使用inline修饰,对编译器来说也只是一个“建议”,具体编译器会自行判断。
那么在swift中如何“建议”呢?

do nothing!啥也不用做,swift会自动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值