Development Tip

선택기 'touchesBegan : withEvent :'로 메서드를 재정의하면 호환되지 않는 유형 '(NSSet, UIEvent)-> ()'가 있습니다.

yourdevel 2020. 10. 15. 21:53
반응형

선택기 'touchesBegan : withEvent :'로 메서드를 재정의하면 호환되지 않는 유형 '(NSSet, UIEvent)-> ()'가 있습니다.


Xcode 6.3. UITextFieldDelegate 프로토콜을 구현하는 클래스 내에서 키보드를 숨기도록 touchesBegan () 메서드를 재정의하고 싶습니다. 함수 사양에서 컴파일러 오류를 피하면 Set 또는 NSSet에서 "터치"를 읽으려는 컴파일러 오류가 있거나, 그렇지 않으면 super.touchesBegan (touches, withEvent : event)에서 오류가 발생합니다. 이러한 조합 중 하나는 Xcode 6.2에서 컴파일되었습니다! (그래서 Swift "Set"에 대한 문서는 어디에 있으며 하나에서 요소를 얻는 방법은 무엇입니까?)

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    // Hiding the Keyboard when the User Taps the Background
        if let touch =  touches.anyObject() as? UITouch {
            if nameTF.isFirstResponder() && touch.view != nameTF {
                nameTF.resignFirstResponder();
            }
        }
        super.touchesBegan(touches , withEvent:event)
    }

시험:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) or
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) 

컴파일러 오류 : 'touchesBegan : withEvent :'선택기를 사용하여 메서드를 재정의하는 중 '(NSSet, UIEvent)-> ()'유형이 호환되지 않습니다.

super.touchesBegan(touches , withEvent:event)

또한 불평

'NSSet'은 암시 적으로 'Set'로 변환 할 수 없습니다. 명시 적으로 변환하기 위해 'as'를 사용하려고 했습니까?

시험:

override func touchesBegan(touches: Set<AnyObject>, withEvent event: UIEvent) 

컴파일러 오류 : 'AnyObject'유형이 'Hashable'프로토콜을 준수하지 않습니다.

시험:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) 

컴파일러 오류

if let touch = touches.anyObject() as? UITouch 

'Set'에는 'anyObject'라는 멤버가 없지만 함수 사양과 super () 호출은 괜찮습니다!

시험:

override func touchesBegan(touches: NSSet<AnyObject>, withEvent event: UIEvent) -> () or
override func touchesBegan(touches: NSSet<NSObject>, withEvent event: UIEvent) 

컴파일러 오류 : 비 제네릭 유형 'NSSet'을 특수화 할 수 없습니다.


스위프트 1.2 (엑스 코드 6.3) 네이티브 도입 Set다리와 유형을 NSSet. 이것은에서 언급 스위프트 블로그 와의 엑스 코드 6.3 릴리스 노트 , 하지만 분명히 아직 공식 문서에 추가되지 않습니다 (갱신 :로 아마드 Ghadiri는 지적 이, 되어 지금 문서화).

이제 UIResponder메서드는 다음과 같이 선언됩니다.

func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)

다음과 같이 재정의 할 수 있습니다.

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let touch = touches.first as? UITouch {
        // ...
    }
    super.touchesBegan(touches , withEvent:event)
}

Swift 2 업데이트 (Xcode 7) : ( Swift 2의 Override func 오류 비교 )

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {
        // ...
    }
    super.touchesBegan(touches, withEvent:event)
}

Swift 3 업데이트 :

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        // ...
    }
    super.touchesBegan(touches, with: event)
}

xCode 7 및 swift 2.0에서는 다음 코드를 사용하십시오.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch =  touches.first{
        print("\(touch)")
    }
    super.touchesBegan(touches, withEvent: event)
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch = touches.first{
        print("\(touch)")
    }
    super.touchesEnded(touches, withEvent: event)
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch = touches.first{
        print("\(touch)")
    }
    super.touchesMoved(touches, withEvent: event)
}

사용 스위프트 3엑스 코드 (8)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
// Don't forget to add "?" after Set<UITouch>
}

이제 여기 Apple API 참조 있으며 xCode 버전 6.3 및 swift 1.2에서 재정의하려면 다음 코드를 사용할 수 있습니다.

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let touch =  touches.first as? UITouch {
         // ...
    }
    // ...
}

2015 년 12 월 19 일 xCode 7.2 Swift 2.1 현재 최신 업데이트입니다.

다음에 이와 같은 오류가 다시 발생하면 함수를 제거하고 "touchesBe ..."를 다시 입력하기 시작하면 xCode가 자동으로 이전 항목을 수정하는 대신 최신 항목으로 완료합니다.

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch: AnyObject! in touches {
        let touchLocation = touch.locationInNode(self)

        //Use touchLocation for example: button.containsPoint(touchLocation) meaning the user has pressed the button.
    }
}

나를 위해 일한 것은 다음과 같습니다.

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        if let touch = touches.first as? UITouch {
            // ...
        }

        super.touchesBegan(touches , withEvent:event!)
    }

Small addition. For swift to compile w/o error, you need to add

import UIKit.UIGestureRecognizerSubclass


Using Swift 4 and Xcode 9

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

}

참고URL : https://stackoverflow.com/questions/28771896/overriding-method-with-selector-touchesbeganwithevent-has-incompatible-type

반응형