目标是将以下条件更新为 Swift 2.2 语法,建议使用 #selector 或显式构造 Selector
。
if activityViewController.respondsToSelector("popoverPresentationController") {
}
但是,使用下面的替换失败并生成错误提示 Argument of#selector cannot refer to a property
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
关于swift - #selector 的参数不能引用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36380829/