Overriding method with selector 'touchesBeganwithEvent' has incompatible type 'NSSet, UIEvent - '
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This compiler error appears when the touchesBegan override signature in Swift does not exactly match UIKit’s expected method. The mismatch is usually caused by old syntax, wrong parameter types, or missing optional markers. The fix is to use the exact responder-method declaration for your Swift version and class type.
Correct Modern Swift Signature
For UIView and UIViewController subclasses in modern Swift, the expected signature is:
Important details:
- '
Set<UITouch>instead ofNSSet' - '
UIEvent?optional event parameter' - first argument label is underscore
- '
overridekeyword is required'
Any deviation can trigger incompatible-type or selector mismatch diagnostics.
Why This Error Happens
Most cases come from copied legacy snippets that used Swift 2 style signatures.
Legacy-style examples that cause errors in current Swift:
Another common issue is using non-optional UIEvent when framework expects optional. Swift override rules are strict and require exact match.
UIView Versus UIViewController Touch Handling
Both can override responder methods, but event flow differs depending on hierarchy.
UIView override is best when:
- building custom controls
- drawing surfaces
- low-level gesture preprocessing
UIViewController override can work, but touches may already be consumed by subviews or recognizers before reaching controller.
If this does not fire, inspect responder chain rather than signature first.
Gesture Recognizers and Responder Interactions
If a gesture recognizer is active, it may intercept touches before your override sees them.
Checklist:
- ensure
isUserInteractionEnabledis true for target view - inspect recognizer properties such as cancellation behavior
- use recognizer delegate for simultaneous recognition cases
In many apps, recognizers are cleaner than raw touch overrides for common gestures such as tap, pan, and long press.
Objective-C Equivalent for Mixed Projects
In Objective-C code, method signature should match UIKit declaration exactly.
In mixed Swift and Objective-C projects, bridge issues can hide real signature mistakes. Confirm each side uses framework-consistent types.
Debugging Workflow
When the compile error persists:
- verify class inheritance from
UIVieworUIViewController - compare signature character by character with Xcode quick help
- clean build folder to remove stale generated interfaces
- confirm no extension with conflicting method declaration exists
Stale caches and accidental duplicate declarations are surprisingly common in large projects.
Best Practices for Touch Overrides
- Always call
superunless you intentionally suppress inherited responder behavior. - Keep logic lightweight to avoid frame drops during rapid touch events.
- Prefer recognizers for high-level interaction and reserve low-level overrides for specialized controls.
This keeps touch handling maintainable and less brittle over time. In large apps, documenting where low-level touch handling is allowed prevents accidental duplication between gesture-based and responder-based interaction layers.
Common Pitfalls
- Using outdated Swift signature syntax from old tutorials.
- Declaring
NSSetinstead ofSet<UITouch>in Swift overrides. - Forgetting optional
UIEvent?and causing signature mismatch. - Assuming controller-level touch override always receives events.
- Ignoring gesture recognizer interception when debugging missing callbacks.
Summary
- The error is caused by method-signature mismatch against UIKit override contract.
- Use modern signature with
Set<UITouch>and optional event parameter. - Validate responder-chain behavior separately from compile-time signature issues.
- Prefer gesture recognizers for common interactions.
- Keep override declarations exact and framework-aligned.

