Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument of #selector cannot refer to a property

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?

like image 459
Crashalot Avatar asked Apr 03 '16 02:04

Crashalot


1 Answers

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
like image 124
smallfinity Avatar answered Oct 11 '22 00:10

smallfinity