属性setter respondsToSelector

时间:2015-03-27 11:45:07

标签: ios swift

如何检查Swift实例是否具有给定的属性设置器?

特别是,在下面的案例中,正确的选择器是什么? (这个单元的方法确实存在于iOS8中,但在iOS7中不存在,所以我想检查它。)

if self.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
    self.preservesSuperviewLayoutMargins = false;
}

1 个答案:

答案 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