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中如何“建议”呢?