8

The goal is to update the below condition to Swift 2.2 syntax, which advises using #selector or explicitly constructing a Selector.

if activityViewController.respondsToSelector("popoverPresentationController") {

}

However, using the below as a replacement fails and generates an error saying Argument of #selector cannot refer to a property

if activityViewController.respondsToSelector(#selector(popoverPresentationController)) {

}

What's the right way to implement this check with #selector?

Crashalot
  • 33,605
  • 61
  • 269
  • 439

1 Answers1

2

You can use the following:

if activityViewController.respondsToSelector(Selector("popoverPresentationController")) {

}

Or if you target iOS only

if #available(iOS 8.0, *) {
    // You can use the property like this
    activityViewController.popoverPresentationController?.sourceView = sourceView
} else {

}

Or if your code is not limited to iOS

#if os(iOS)
    if #available(iOS 8.0, *) {
        activityViewController.popoverPresentationController?.sourceView = sourceView
    } else {

    }
#endif
smallfinity
  • 141
  • 5