2016-03-25 6 views
-2

私はといくつかの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") 
     } 
    } 
} 
+0

をオンになっていますか?セレクタ構文が7.3のために変更されました – dpassage

+2

このパターンを迅速に回避する必要があります。代わりにオプションの連鎖を使用してください。 – Shripada

+0

私はXCode 7.2.1を使用しています。 – dichen

答えて

4

は、現在(スウィフト2.2で非推奨と考えられていますXcode 7.3)、Swift 3で削除されます。これは、エラーが起こりやすいためです。

新しい構文がSwift 2.2:#selector()で導入されました。タイプセーフで、入力時にXcodeは自動補完機能を提供します。あなたのコードのために、Selector(String)通話:

Selector("doSomethingWithoutParams") 
Selector("doSomethingWithOneParam:") 
Selector("doSomethingWithTwoParams::") 

を置き換えてください:あなたはXcodeのどのバージョンを

#selector(ViewControllerDelegate.doSomethingWithoutParams) 
#selector(ViewControllerDelegate.doSomethingWithOneParam(_:) 
#selector(ViewControllerDelegate.doSomethingWithTwoParams(_:secondParam:) 
0

、およびクレジットがhttps://stackoverflow.com/a/24168825/3543150に行く:すべての機能は、デリゲートオブジェクトによって実装されてい

注、ここに私のコードです。

あなたはセレクタ文字列内のParameterNameを指定する必要があります。

if (delegate != nil && delegate!.respondsToSelector(Selector("doSomethingWithTwoParams:secondParam:"))) { 

     // Entered here now 

     NSLog("doSomethingWithTwoParams is implemented") 

     delegate!.doSomethingWithTwoParams!(self, secondParam: "dummy") 
    } 
    else{ 

     NSLog("doSomethingWithTwoParams is not implemented") 
    } 

そして、それはチェックのためのオプションのチェーンを使用してよりよいです:Selector(String)を使用して

// Nicer way 
    guard let _ = delegate?.doSomethingWithTwoParams?(self, secondParam: "dummt") else { 

     NSLog("doSomethingWithTwoParams is not implemented, do some default stuff") 

     return 
    } 
+1

http://www.apeth.com/swiftBook/apa.html#_selectorsしかし、真剣に、この問題が完全になくなるXcode 7.3にアップデートしてください。 – matt

+0

ありがとう、すぐにそれを行います。 – dichen