1

How to check in Swift whether an instance have a given property setter or not?

Especially, what is the correct Selector in the case below? (This method of the cell does exist in iOS8 but does not exist in iOS7, so I want to check it).

if self.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
    self.preservesSuperviewLayoutMargins = false;
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Balazs Nemeth
  • 2,333
  • 19
  • 29

1 Answers1

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
}

Note: Property 'drawsAsynchronously' of CAShapeLayer class is available from iOS 6.0

@available(iOS 6.0, *)
open var drawsAsynchronously: Bool
Grand M
  • 193
  • 1
  • 8