2017-12-30 48 views
1

나는 생각할 수있는 모든 순열을 시도해보고 Swift에 관한 모든 종류의 문서를 읽었지만 어디에도 없습니다. 여기 코드는 다음과 같습니다간단한 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. 이