2017-12-30 39 views
1

私は考えることができるすべての並べ替えを試してみました。Swift #selectorsに関するすべての種類のドキュメントを読んでいますが、私はどこにもいません。ここでは、コードです:簡単なSwiftの#selectorをコンパイルできない

class AFSelectionState: GKComponent { 
    let clearSelectionIndicator: (Set<Int>?) -> Void 
    let setSelectionIndicator: (Set<Int>) -> Void 

    init(setSelectionIndicator: @escaping (Set<Int>) -> Void, clearSelectionIndicator: @escaping (Set<Int>?) -> Void) { 
     self.clearSelectionIndicator = clearSelectionIndicator 
     self.setSelectionIndicator = setSelectionIndicator 
    } 
} 

class GameScene: SKScene, SKViewDelegate { 
    var selectionState: AFSelectionState! 

    override func sceneDidLoad() { 
     ... 

/****************** Compiler errors coming up **************** 
** 
** Tried #selector(setSelectionIndicator_(_:)) 
** Got "Cannot convert value of type 'Selector' to expected 
** argument type '(Set<Int>) -> Void'" 
** From what I've read, the above should be working, but you know 
** how it is when people say "should". 
** 
** Tried #selector(setSelectionIndicator_(_:) ->()) 
** Got "Expected type before '->'". 
** 
** Tried all sorts of other stuff. There's something about 
** selectors that I'm missing. 
** 
*********************** Et cetera! *************************/ 

     selectionState = AFSelectionState(

      setSelectionIndicator: #selector(setSelectionIndicator_(_:)), 
      clearSelectionIndicator: #selector(clearSelectionIndicator_(_:)) 

     ) 

     ... 
    } 
} 

extension GameScene { 
    @objc func setSelectionIndicator_(_ selectedIndexes: Set<Int>) -> Void { 
     ... 
    } 

    @objc func clearSelectionIndicator_(_ indexes: Set<Int>?) -> Void { 
     ... 
    } 
} 

答えて

2

Selectorは基本的に文字列ではなく、閉鎖、またはブロックまたは何でも実行することができ、コードの一部です。必要なものを達成するには、これを試してください:

selectionState = AFSelectionState(

    setSelectionIndicator: self.setSelectionIndicator_, 
    clearSelectionIndicator: self.clearSelectionIndicator_ 

) 

P.S.

selectionState = AFSelectionState(setSelectionIndicator: setSelectionIndicator_, clearSelectionIndicator: clearSelectionIndicator_) 

あなた混在セレクタとクロージャ:あなたはこれが正しい構文ですこの

関連する問題