2016-04-03 20 views
8

目標是將以下條件更新爲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的正確方法?

+0

popOverPresentationController是屬性不一個功能。對? – Darko

回答

2

您可以使用以下方法:

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