2017-12-30 11 views
1

Ich habe versucht, jede Permutation, die ich mir vorstellen kann, und lesen Sie alle Arten von Doc über Swift #selectors, aber ich komme nirgendwo hin. Hier ist der Code:Kann nicht einfach Swift #Selector zu kompilieren

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 { 
     ... 
    } 
} 

Antwort

2

Selector ist im Grunde eine Zeichenfolge, kein Verschluss oder Block oder was auch immer Stück Code, der ausgeführt werden könnte. Um zu erreichen, was Sie brauchen, versuchen Sie Folgendes:

selectionState = AFSelectionState(

    setSelectionIndicator: self.setSelectionIndicator_, 
    clearSelectionIndicator: self.clearSelectionIndicator_ 

) 

P.S. stellen Sie sicher, nicht Referenzzyklus mit diesem

Verwandte Themen