Override Func Error in Swift 2

Override function error in swift

For now the method in the extension must be marked with @objc to allow overriding it in subclasses.

extension UIViewController: XProtocol {

@objc
func dealError(error: ErrorResultType) {
// do something
}
}

But that requires all types in the method signature to be Objective-C compatible which your ErrorResultType is not.
Making your ErrorResultType a class instead of a struct should work though.

Override func error in Swift 2

You're getting your first error because much of Cocoa Touch has been audited to support Objective-C generics, meaning elements of things like arrays and sets can now be typed. As a result of this, the signature of this method has changed and since what you've written no longer matches this, you're given an error explaining that you've marked a method as override but it doesn't actually match any methods from the super class.

Then when you remove the override keyword, the error you're getting is letting you know that you've made a method that has a conflicting Objective-C selector with the real touches began method (unlike Swift, Objective-C doesn't support method overloading).

The bottom line is, in Swift 2, your touches began override should look like this.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// stuff
}

For more information on what Objective-C generics mean for your Swift code, I suggest you take a look at the Lightweight Generics section in the prerelease version of Using Swift with Cocoa and Objective-C. As of now on pages 33 & 34.

Override func Error! Method does not override any method from its superclass

The error is coming because your method signature is different than the actual ones. You should use the exact method signature for overriding (Either you should use auto-complete feature provided in Xcode or refer the documentation)

Touches Began:

override func touchesBegan(_ touches: Set<UITouch>, 
with event: UIEvent?)
{
}

In Swift 3 supportedInterfaceOrientations is no longer a method, it's a computed property, so it's signature becomes:

override var supportedInterfaceOrientations : UIInterfaceOrientationMask
{
}

Swift 3- How to override a method in subclass

In Swift 3 caretRectForPosition(position:) is changed to caretRect(for:).

override func caretRect(for position: UITextPosition) -> CGRect {
return CGRect.zero
}

Check Apple Documentation for more detail on UITextInput.

Issue between override and non-override function in swift

There is a mismatch of UIResponder method signature and your function implementation. UIResponder has optional Event as following:
func remoteControlReceibedWithEvent(_ event: UIEvent?)

So it can not override as there is no function with non-optional argument, but if you remove override it will conflict with ObjC implementation, as selector names are the same.



Related Topics



Leave a reply



Submit