如何检查Swift实例是否具有给定的属性设置器?
特别是,在下面的案例中,正确的选择器是什么? (这个单元的方法确实存在于iOS8中,但在iOS7中不存在,所以我想检查它。)
if self.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
self.preservesSuperviewLayoutMargins = false;
}
答案 0 :(得分:0)
import UIKit
var shapeLayer = CAShapeLayer()
// Option #1
if shapeLayer.responds(to: #selector(setter: CAShapeLayer.drawsAsynchronously)) {
shapeLayer.drawsAsynchronously = true
}
// Option #2
if #available(iOS 6.0, *) {
shapeLayer.drawsAsynchronously = true
}
注意:iOS 6.0中提供了CAShapeLayer类的属性“drawAsynchronously”
@available(iOS 6.0, *)
open var drawsAsynchronously: Bool