Objective-C
iOS Development
Method Overriding
Programming Errors
UIKit

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:

swift
1import UIKit
2
3class TouchView: UIView {
4    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
5        super.touchesBegan(touches, with: event)
6        print("touch began", touches.count)
7    }
8}

Important details:

  • 'Set<UITouch> instead of NSSet'
  • 'UIEvent? optional event parameter'
  • first argument label is underscore
  • 'override keyword 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:

swift
// old style, not valid for current Swift overrides
// override func touchesBegan(touches: NSSet, withEvent event: UIEvent)

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.

swift
1class TouchController: UIViewController {
2    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
3        super.touchesBegan(touches, with: event)
4        print("controller touch")
5    }
6}

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 isUserInteractionEnabled is 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.

objective-c
1- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
2    [super touchesBegan:touches withEvent:event];
3    NSLog(@"touch began");
4}

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:

  1. verify class inheritance from UIView or UIViewController
  2. compare signature character by character with Xcode quick help
  3. clean build folder to remove stale generated interfaces
  4. confirm no extension with conflicting method declaration exists
bash
# Xcode menu equivalent: Product -> Clean Build Folder

Stale caches and accidental duplicate declarations are surprisingly common in large projects.

Best Practices for Touch Overrides

  • Always call super unless 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 NSSet instead of Set<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.

Course illustration
Course illustration

All Rights Reserved.