2017-04-13 50 views
3

我在Swift 3中搜索了很多選擇器方法,但我對它有很多困惑。swift 3選擇器與參數

1)Selector & #selector有什麼區別?

2)如果我用Selector寫的,這個函數概述意味着不可用?

3)如何通過#selector方法傳遞參數。

我的代碼

let button = UIButton()  
button.addTarget(self, action: #selector(getData(_:true)), for: .touchUpInside) 
button.addTarget(self, action: Selector(), for: .touchUpInside) 

func getData(_ isShowing:Bool){ 

    } 

你能不能幫我清除我的困惑?

感謝您的寶貴時間

+0

與這一個http://stackoverflow.com/questions/41520683/swift-3-unrecognized-selector-sent-to-instance-uibutton –

+0

關於質詢3.比較只需使用'#selector( getData)'不需要指定參數。 –

+0

@LeoDabus,謝謝你的回答,但我必須通過一個值檢查條件。 – user1673099

回答

1

我相信#selector僅僅是一個語言結構創建Selector類型的對象。你想使用#selector作爲編譯器實際檢查方法是否存在任何地方,其中Selector("abc")你只是運行構造函數,它沒有驗證。

2

問題的答案:

  1. 選擇是一種類型。 (以表明它是一個函數類型)。而#選擇器是調用一個函數。 #selector - >將返回Selector類型。 #selector檢查功能名稱是否存在任何功能
  2. 第一個答案將闡明此問題
  3. 您可以像這樣通過發件人發送價值。例如:button.layer.setValue(forKey:"someKey")
+2

至於#3:你不能爲任意值做到這一點,只能爲那些符合KVO的。關於OP的要求,他不能通過他的'布爾'這種方式。 – Losiowaty