2

I am getting this Swift error:

Method does not override any method from its superclass!

Why is this appearing? The relevant code is below:

class TouchyView: UIView {

override func touchesBegan(touches: NSSet?, withEvent event: UIEvent) {
    updateTouches(event.allTouches())
}
override func touchesMoved(touches: NSSet?, withEvent event: UIEvent) {
    updateTouches(event.allTouches())
}
override func touchesEnded(touches: NSSet?, withEvent event: UIEvent) {
    updateTouches(event.allTouches())
}
override func touchesCancelled(touches: NSSet!!, withEvent event: UIEvent) {
    updateTouches(event.allTouches())
}

var touchPoints = [CGPoint]()

func updateTouches( touches: NSSet? ) {
    touchPoints = []
    touches?.enumerateObjectsUsingBlock() { (element,stop) in
        if let touch = element as? UITouch {
            switch touch.phase {
                case .Began, .Moved, .Stationary:
                    self.touchPoints.append(touch.locationInView(self))
                default:
                    break
            }
        }
     }
    setNeedsDisplay()
}
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Karl Kivi
  • 145
  • 11
  • possible duplicate of [Swift protocols: method does not override any method from its superclass](http://stackoverflow.com/questions/24380681/swift-protocols-method-does-not-override-any-method-from-its-superclass) – Pietro Saccardi Sep 26 '15 at 16:23
  • I don't understand, I've looked that post and tried that solution, but it is not working on my code :( – Karl Kivi Sep 26 '15 at 16:30
  • Have you tried perhaps this? http://stackoverflow.com/a/30892467/1749822 -- there are many questions around that cover the same topic. – Pietro Saccardi Sep 26 '15 at 16:43
  • I found the solution! – Karl Kivi Sep 26 '15 at 17:50

1 Answers1

1

Inheritance : If you have a class with a method

class myClass {

func abc () {
   // do something
}

and you make another class which inherits from the first :

class myOtherClass :myClass {

// do whatever stuff in this class

}

that second class will also have that abc method from the first class. If you want however to change the functionality of abc in that second class you have to use the

override

keyword.

class myOtherClass :myClass {

override func abc () {
    // do something else
}


// do whatever stuff in this class

}

It's great that swift makes you add that override keyword, you won't accidentally override methods from the first class (which often happens if the name is quite generic).

You can not however use override (and it's of no use) if the method doesn't exists in the first class. And this is your case.

Hope this helps and if this is not clear I advise you to read a book on Object Oriented Programming

Glenn
  • 2,808
  • 2
  • 24
  • 30