私はといくつかのoptional
の関数を持っています。私の目標は、デリゲートオブジェクトがデリゲート関数を実装しているかどうかをチェックし、デリゲートオブジェクトを実行するかどうかをチェックすることです。respondsToSelectorはSwiftで複数のパラメータを持つセレクタを認識できません
私はこの目的でrespondsToSelector
を使用していますが、パラメータが1つでも唯一でも、2つのパラメータでは機能しない場合は正常に動作します。
助けてもらえますか?前もって感謝します。 、私は私の自己で答えを見つけたと思う@Shripadaのおかげ
@objc protocol ViewControllerDelegate: NSObjectProtocol {
optional func doSomethingWithoutParams()
optional func doSomethingWithOneParam(controller: ViewController)
optional func doSomethingWithTwoParams(controller: ViewController, secondParam: String)
}
class ViewController: UIViewController {
weak var delegate: ViewControllerDelegate?
@IBAction func onButtonClicked(sender: AnyObject){
if (delegate != nil && delegate!.respondsToSelector(Selector("doSomethingWithoutParams"))) {
// Entered here
}
else{
NSLog("doSomethingWithoutParams is not implemented")
}
if (delegate != nil && delegate!.respondsToSelector(Selector("doSomethingWithOneParam:"))) {
// Entered here
}
else{
NSLog("doSomethingWithOneParam is not implemented")
}
if (delegate != nil && delegate!.respondsToSelector(Selector("doSomethingWithTwoParams::"))) {
NSLog("doSomethingWithTwoParams is implemented")
}
else{
// Entered here
NSLog("doSomethingWithTwoParams is not implemented")
}
}
}
をオンになっていますか?セレクタ構文が7.3のために変更されました – dpassage
このパターンを迅速に回避する必要があります。代わりにオプションの連鎖を使用してください。 – Shripada
私はXCode 7.2.1を使用しています。 – dichen