Overriding method with selector 'touchesBegan:withEvent:' has incompatible type '(NSSet, UIEvent) -> ()'

ios override method from superclass
override method in objc
override class method objc
objective c override selector
objective c class method override
add method to class objective-c
objective-c add method to existing class
objective-c extension

Xcode 6.3. Within a class implementing UITextFieldDelegate protocol, I would like to override touchesBegan() method to possibly hide the keyboard. If I avoid a compiler error in the function spec, then there is a complier error trying to read the "touch" from the Set or NSSet, or else the super.touchesBegan(touches , withEvent:event) throws an error. One of these combinations compiled in Xcode 6.2! (So where is documentation to Swift "Set" and how to get an element from one?)

 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)
    }

Try:

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

Compiler error: Overriding method with selector 'touchesBegan:withEvent:' has incompatible type '(NSSet, UIEvent) -> ()' and

super.touchesBegan(touches , withEvent:event)

also complains

'NSSet' is not implicitly convertible to 'Set'; did you mean to use 'as' to explicitly convert?

Try:

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

Compiler error: Type 'AnyObject' does not conform to protocol 'Hashable'

Try:

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

Compiler error at

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

'Set' does not have a member named 'anyObject' BUT the function spec and call to super() are OK!

Try:

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

Compiler error: Cannot specialize non-generic type 'NSSet'

Swift 1.2 (Xcode 6.3) introduced a native Set type that bridges with NSSet. This is mentioned in the Swift blog and in the Xcode 6.3 release notes, but apparently not yet added to the official documentation (update: As Ahmad Ghadiri noted, it is documented now).

The UIResponder method is now declared as

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

and you can override it like this:

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

Update for Swift 2 (Xcode 7): (Compare Override func error in Swift 2)

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

Update for Swift 3:

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

Overriding methods without making a subclass � GitHub, This first gets a function pointer to the super method and then you call it. */. - (void *)mh_superForSelector:(SEL)selector;. @end. NSObject+MHOverride.m. CSS selector overriding. Ask Question Asked 7 years, 7 months ago. Active 3 years, 5 months ago. Viewed 7k times 4. 1. I'm trying to merge two CSS files from

With xCode 7 and swift 2.0, use following code:

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)
}

Method overriding, Accessor method � Naming Conventions Interface � Implementation � Class method Root class � Selector � Getting a Selector � Using a� However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method. Constructors cannot be overridden. Using the super Keyword. When invoking a superclass version of an overridden

Using Swift 3 and Xcode 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>
}

Overriding method with selector 'touchesBegan , Overriding methods in Swift extensions, You can only override a superclass method i.e. load() initialize() in an extension of a subclass if the method is Objective-C� Method overriding is used to provide the specific implementation of a method which is already provided by its superclass. Method overriding is used for runtime polymorphism; Rules for Java Method Overriding. The method must have the same name as in the parent class; The method must have the same parameter as in the parent class. There must be

It is now in the Apple API reference here and for overriding in xCode version 6.3 and swift 1.2 you can use this code:

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

Overriding method with selector 'touchesBegan , Overriding method with selector 'touchesBegan:withEvent:' has incompatible type '(NSSet, UIEvent) -> ()'. override method in objc objective-c extension override� Method overriding is one of the way by which java achieve Run Time Polymorphism.The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to

The current one right now for the newest update as of xCode 7.2 Swift 2.1 on Dec 19, 2015.

Next time you get an error like this again, remove the function and start typing it again "touchesBe..." and xCode should automatically complete it to the newest one for you instead of trying to fix the old one.

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.
    }
}

Possible to accidentally override a private method?, Is it possible for a subclass to unknowingly override a private method of a a method in a subclass (or extension!) with the same selector you'll� Rules for method overriding: In java, a method can only be written in Subclass, not in same class. The argument list should be exactly the same as that of the overridden method. The return type should be the same or a subtype of the return type declared in the original overridden method in the super class. The access level cannot be more

Subclassing Objective-C Classes, Methods that are overridden will infer their signatures from the base class or protocols This string is also used as the selector of the method in Objective-C. When overriding one method with another, the signatures of the two methods must be identical (and with same visibility). In C#, class methods, indexers, properties and events can all be overridden. Non-virtual or static methods cannot be overridden. The overridden base method must be virtual, abstract, or override.

@objc Warnings Upgrading To Swift 4, For example, when overriding an @objc method or implementing an the obvious places such as methods that are the target for a #selector . Method overriding in Java is a concept based on polymorphism OOPS concept which allows programmer to create two methods with same name and method signature on interface and its various implementation and actual method is called at runtime depending upon type of object at runtime.

Selector Patterns for Lightning Component, But I am getting the following errors. @Override specified for non-overriding method: List soco.DataImportController.getSObjectFieldList(). Method Overriding in C# is similar to the virtual function in C++. Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called as method overriding.

Comments
  • Thanks Martin R, All I had to do was change my NSSet to Set<NSObject> and my code is functioning.
  • Hi @Martin Why super.touchesBegan(touches , withEvent:event) is need? I am still not understand Swift very well.
  • I feel like it needs to be this: super.touchesBegan(touches, withEvent:event!) for xCode 7.2
  • @EugeneGordin: Why do you think so? The last method still compiles with Xcode 7.2.
  • it was complaining for me...value of optional type not unwrapped
  • I can't build the project. Getting error Method does not override any method from its superclass for the touchesBegan function. I tried to subclass from UIGestureRecognizer and UTapGestureRecognizer. Xcode 7.1
  • use this stackoverflow.com/questions/30892254/… to check what going on with your code
  • Interesting that the online docs are correct but the version included in the Xcode help system is out-of-date. I guess I need to get in the habit of searching for this stuff online.
  • If you are on Xcode 6.3 and Swift 1.2 your code will not work. This post if referencing Xcode 6.3 and Swift 1.2.
  • You are right, I updated Xcode today and it immediately gave error.
  • Add comments, explain your answer, read how to answer.