2016-08-30 3 views
0

CBPeripheralDelegate에서 선택자를 정의하려면 func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?)입니다. swift3에서재정의 방법에 대해 swift3에서 선택기를 정의하는 방법

, 그것은 peripheral(_: didUpdateValueFor:error)로 변경, 내가 컴파일 오류가 발생합니다이 #selector(CBPeripheralDelegate.peripheral(_:didUpdateValueFor:error:)) 같은 선택을 정의하려고 그래서 때 그것은 func peripheral(peripheral: CBPeripheral, didUpdateValueForDescriptor descriptor: CBDescriptor, error: NSError?)

과 동일하다 : ambiguous use합니다.

그리고 나는 doc처럼 정의하려고 시도합니다. #selector(((CBPeripheralDelegate.peripheral(_:didUpdateValueFor:error:)) as (CBPeripheralDelegate) -> (CBPeripheral, CBCharacteristic, NSError) -> Void), 중 하나가 실패했습니다.

그래서 swift3에서 선택기를 정의하는 올바른 방법은 무엇입니까?

답변

1

이것은 현재 #selector 표기법의 결함 일 수 있으며 Apple 또는 swift.org으로 버그 리포트를 보내야합니다. (또는 뭔가를 놓친 것일 수도 있습니다 ...)

이 문제를 해결하려면 프로토콜을 준수하는 클래스를 정의하고 선택기를 만들려는 메서드를 정의하십시오.

class TheClass: NSObject, CBPeripheralDelegate { 
    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { 
     //No need to actually implement the method 
    } 
} 

그리고 당신의 #selector()에서 클래스 이름 사용 : 당신은 이미 방법을 구현하는 클래스를 가질 수있다

#selector(TheClass.peripheral(_:didUpdateValueFor:error:)) 

을, 당신은 그것의 클래스 이름을 사용할 수 있습니다.

Objective-C 선택자는 클래스 이름 정보를 유지하지 않으므로 메서드를 구현할 수있는 모든 클래스에 사용할 수 있습니다. 당신이 프로토콜을 준수 및 방법에 대한 정의가 클래스 내부에서 선택을 확인하려면

, 당신은 그것을 쓸 수 있습니다 :

#selector(peripheral(_:didUpdateValueFor:error:)) 
관련 문제