swift - #selector 的参数不能引用属性

标签 swift uiactivityviewcontroller

目标是将以下条件更新为 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/

相关文章:

swift - Xcode 7.3.1 更新至 8,带有 cocoapods 的 Swift2 项目即将构建

ios - 在不启动应用程序的情况下显示主屏幕操作

ios - 如何防止 Action 扩展出现在我自己的 iOS 应用程序中?

ios - 如何知道用户是否共享了我的应用程序?

ios - PopOverViewController 中的 UIActivityViewController 在 Swift 中崩溃

ios - UIActivityViewController 错误 : Failed to determine whether URL is managed by a file provider

arrays - Swift 过滤数组中的自定义对象

ios - Rx swift : Connect to Connectable Observable multipe times

ios - 将捕获的图像添加到下一个 View Swift 3 Xcode 8

ios - 在 NSUserDefaults 或文档目录中保存和读取图像数组及其数据