2016-07-28 3 views
2

"showListenerDidReceiveNotification :"이라는 관련 선택기를 사용하여 "showPhotoForMoodNotification"이라는 알림의 관찰자가되는 SKScene이 있습니다. "선택기 사용 방법 : Swfit에서 예외를 throw하는 함수로

func eventListenerDidReceiveNotification(notif:NSNotification) throws { } 

을하지만 알림이 SKScene에 의해 수신 될 때, 컴파일러는이의 서명을 연결하지 않는 것으로 나타났습니다 :

eventListenerDidReceiveNotification 다음과 같이 던져 예외 수있는 함수로 선언 thisL

NSNotificationCenter.defaultCenter().addObserver(self, selector: "eventListenerDidReceiveNotification:", name: "showPhotoForMoodNotification", object: nil) 

내가 오류처럼 보인다라는 addObserver의 선택 ", eventListenerDidReceiveNotification"의 서명과 방법은 "이것이다 : enter image description here

내 생각에 "throws"부분을 "throws"부분에서 제거하면 메서드의 서명 중 "throws"부분이 nsnotification "addObserver"호출의 "selector"부분과 호환되지 않습니다. "메소드 선언, 모든 것이 작동합니다.

그래서이 메서드를 예외를 throw하는 메서드로 설명하기 위해 addObserver "selector"파트에 더 추가해야합니까?

감사

답변

0

가능한 대답 here. BTW, 스위프트 2.2 (실제로, 당신이 사용하는 버전을 모르겠다) new syntax for selectors이 그것을 사용하는 것이 좋습니다 방법입니다.

사실 그냥 코드 테스트 (IBAction를 스토리 보드에 버튼 TouchUpInside 이벤트에 접속)과 그 일 :

override func viewDidLoad() { 
    super.viewDidLoad() 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(test(_:)), name: "TestNotification", object: nil) 
} 

@objc private func test(notification: NSNotification) throws { 
    print("notification") 
} 

@IBAction private func fireNotification() { 
    NSNotificationCenter.defaultCenter().postNotificationName("TestNotification", object: nil) 
} 
0

IIRC,

func f(x: T) throws -> U 

같은 신속한 방법을 객관적 C로 표시하면

- (nullable U *)fWithX:(T *)x error:(NSError **)errorPtr; 

그래서을 추가 할 수 있습니다. 선택기에서 0 부분.

편집 :

그리고

func f() throws -> U 

- (nullable U *)fAndReturnError:(NSError **)errorPtr; 
을이되다
관련 문제