iOS知识小集之Swift闭包作为Objective-C方法参数
Swift闭包作为Objective-C方法参数
在Swift开发时,有时我们需要调用带有Block参数的Objective-C方法,比如,我们通过perform(_:with:afterDelay:inModes:)来在特定的Runloop模式下运行某selector,如下所示,doAnimation方法接收一个Optional的Block,注意,该方法标注了@objc,所以是Objective-C方法,animationBlock参数为Objective-C的Block,那么我们怎么将Swift的Closure转化为Block呢,方法就是使用@convention(block)来声明兼容Objective-C Block的Closure,如下[1]所示:
1 | class LocationMessageCell: UICollectionViewCell { |
除了显式的用@convention(block)来声明,我们也可以直接使用闭包,如:
1 | UIView.animate(withDuration: 0.1, animations: { |
注意,之所以我没有在之前的例子进行修改,是因为func perform(_ aSelector: Selector, with anArgument: Any?, afterDelay delay: TimeInterval, inModes modes: [RunLoopMode])的anArgument参数类型为Any?,编译器没有进行自动转换,这种情况只能显式的声明@convention(block)。