ios - 属性 setter responseToSelector

标签 ios swift

如何在 Swift 中检查实例是否具有给定的属性 setter ?

特别是,在下面的例子中正确的选择器是什么? (cell的这个方法iOS8有,iOS7没有,所以想查一下)。

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

最佳答案

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
}

注意:CAShapeLayer 类的属性 'drawsAsynchronously' 可从 iOS 6.0 开始使用

@available(iOS 6.0, *)
open var drawsAsynchronously: Bool

关于ios - 属性 setter responseToSelector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29299945/

相关文章:

ios - iOS中的HeightForRowAt和estimatedHeightForRowAt

ios - UIResponder didNotRecognizeSelector:应用程序因此错误而崩溃

swift - 数组的扩展,其中元素是可选的

iphone - 将选择器上的分数格式化为最小公分母?

ios - 在 ViewDidAppear 中填充 TableView

ios - 如何使用 Alamofire 从数组中获取数据以显示在 Collection Cell 中?

ios - Xcode 8 和 Swift 3 中 Unresolved 标识符错误

iphone - CouchDB移动稳定还是可以在ios生产应用中使用?

iphone - 带有 nsdictionary 的 ios nsarrya 有 <null> 元素

ios - iOS 中的 JSON 错误 : Invalid top-level type in JSON write