Swift protocols: method does not override any method from its superclass
Asked Answered
A

1

56

Since Xcode 6 still has a lots of bugs with Swift, I'm not sure is it one or I'm missing something. My class adopts protocol NSLayoutManagerDelegate. But it seems impossible to override method I need. I do as documentation describes:

override func layoutManager(_ aLayoutManager: NSLayoutManager!,
        didCompleteLayoutForTextContainer aTextContainer: NSTextContainer!,
        atEnd flag: Bool) {

    }

But I get error here: method does not override any method from its superclass. What should I do?

Accept answered 24/6, 2014 at 7:23 Comment(0)
C
121

You're implementing a method from the protocol, yes, but it's not an override. Just remove the override keyword. An override is when your superclass also implements that method and you're providing a version that replaces or modifies the behavior of the superclass implementation. That's not what's happening here.

Crankshaft answered 24/6, 2014 at 7:26 Comment(4)
It's not a dumb question, because Xcode 6 auto-completion put the "override" for us, causing the confusion :-)Lawsuit
This is a shame though, as it means there's no compile-time check that ensures you've got the signature right...Edmondedmonda
Yeah, I was thinking this is a bad thing, since there’s no compile-time check that the signature is correct. But, since in Swift all protocol methods are required, you will get some kind of compile-error if you haven’t implemented all required methods.Pitfall
@Pitfall yes, but you do not get a warning if you implement methods defined in a protocol but don't implement the protocol itself.Suppression

© 2022 - 2024 — McMap. All rights reserved.