신속한 2.2 이전에 완벽하게 작동하는 데 사용되는 프로토콜 확장이 있습니다.
이제 new를 사용하라는 경고가 #selector
있지만 추가하면
Objective-C 선택기로 선언 된 메서드가 없습니다.
이 몇 줄의 코드에서 문제를 재현하려고했습니다.이 코드는 쉽게 복사하여 놀이터에 붙여 넣을 수 있습니다.
protocol Tappable {
func addTapGestureRecognizer()
func tapGestureDetected(gesture:UITapGestureRecognizer)
}
extension Tappable where Self: UIView {
func addTapGestureRecognizer() {
let gesture = UITapGestureRecognizer(target: self, action:#selector(Tappable.tapGestureDetected(_:)))
addGestureRecognizer(gesture)
}
}
class TapView: UIView, Tappable {
func tapGestureDetected(gesture:UITapGestureRecognizer) {
print("Tapped")
}
}
프로토콜의 해당 메서드에 추가하라는 제안도 @objc
있지만 그렇게하면이를 구현하는 클래스에도 추가하라는 메시지가 표시되지만 일단 추가하면 클래스가 더 이상 프로토콜을 따르지 않습니다. 프로토콜 확장에서 구현을 보지 못하는 것 같습니다.
이것을 올바르게 구현하려면 어떻게해야합니까?
비슷한 문제가있었습니다. 여기 내가 한 일이 있습니다.
그런 다음 Self를 사용했습니다. #selector에서.
@objc public protocol UpdatableUserInterfaceType {
optional func startUpdateUITimer()
optional var updateInterval: NSTimeInterval { get }
func updateUI(notif: NSTimer)
}
public extension UpdatableUserInterfaceType where Self: ViewController {
var updateUITimer: NSTimer {
return NSTimer.scheduledTimerWithTimeInterval(updateInterval, target: self, selector: #selector(Self.updateUI(_:)), userInfo: nil, repeats: true)
}
func startUpdateUITimer() {
print(updateUITimer)
}
var updateInterval: NSTimeInterval {
return 60.0
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다