目标是将以下条件更新为
Swift 2.2语法,该语法建议使用#selector或显式构建Selector.
if activityViewController.respondsToSelector("popoverPresentationController") {
}
但是,使用以下作为替换失败并生成错误,说#selector的参数不能引用属性
if activityViewController.respondsToSelector(#selector(popoverPresentationController)) {
}
使用#selector实现此检查的正确方法是什么?
您可以使用以下内容:
if activityViewController.respondsToSelector(Selector("popoverPresentationController")) {
}
或者,如果您只针对iOS
if #available(iOS 8.0,*) {
// You can use the property like this
activityViewController.popoverPresentationController?.sourceView = sourceView
} else {
}
或者,如果您的代码不仅限于iOS
#if os(iOS)
if #available(iOS 8.0,*) {
activityViewController.popoverPresentationController?.sourceView = sourceView
} else {
}
#endif