Reputation: 145
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()
}
Upvotes: 2
Views: 427
Reputation: 2806
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
Upvotes: 1