#selector的参数不能引用属性
问题描述:
目标是将以下条件更新为Swift 2.2语法,该语法建议使用#selector or explicitly constructing a 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
popOverPresentationController是属性不一个功能。对? – Darko