How to Override Private Method and Call Super in Swift

how to override private method and call super in swift?

You must deceive the Swift compiler that such method exists. For that you can simply add an Object-C extension declaration to the bridging header of your project:

@import UIKit;

@interface UINavigationController(PrivateMethods)

- (void)_startCustomTransition;

@end

Now, you can override the method without problems:

class MyNavigationController: UINavigationController {
override func _startCustomTransition() {
super._startCustomTransition()
}
}

EDIT: You have to declare the method in an Objective-C extension, because I see no way to declare a method in a Swift extension without implementing it. Suggestions are very welcome. :)

How to override private method and property from superclass in objective c

You can do as below, but you should not do.

You can call the private method by using selectors, but it will show you a warning which you can disable.

    SEL selector = NSSelectorFromString(@"privateMethod");
if ([a respondsToSelector:selector]) {
NSLog(@"yes privateMethod exists");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector:selector];
#pragma clang diagnostic pop
}

Swift calling subclass's overridden method from superclass

Make sure that the object's class is SubclassViewController. Otherwise, it will not have any knowledge of the method which is overriden by subclass

Access private methods of superclass in Swift

You can have a subclass access its superclass's private members BUT it has to be in the same file as the super class. Privacy applies outside the file. Anything within a file can access private members. This is not intuitive. Here is a simple playground to demonstrate. Since Bar is declared in the same file, it can access Foo's private x member.
Sample Image

from Swift Programming Language (Swift 2.1), Access Levels, "Private access restricts the use of an entity to its own file."

When to use super when overriding ios methods

There isn't really a rule of thumb for whether or not you should call the super implementation of a given method. This is something that should be determined on a case by case basis, depending on recommendations from the documentation for the superclass' implementation of that method, and your requirements.

Just to explain why the two examples you included might be the way they are, we can look at the documentation for -[UIView drawRect:], which states:

If you subclass UIView directly, your implementation of this method
does not need to call super. However, if you are subclassing a
different view class, you should call super at some point in your
implementation.

and the documentation for -[NSObject awakeFromNib], which states:

You must call the super implementation of awakeFromNib to give parent
classes the opportunity to perform any additional initialization they
require. Although the default implementation of this method does
nothing, many UIKit classes provide non-empty implementations.

Most of the time, it's probably a safe bet that you should call the super implementation of a method (see comments below). However, be warned, some methods require you to call their super implementation, some don't, and some even require you to call the super implementation at a specific point in your override. So remember, when in doubt, always consult the documentation.

Initializing variable in a private function when inheriting from NSObject

initializeMyVar()
super.init()

It has no connection with NSObject. It's general rule when you're calling init of superclass; you can't call method of self before you initialize your object or before you assign all non-optional variables

_myVar = 4
super.init()

super.init()
initializeMyVar()

In second case where you assign variable after super.init(), you don't give value to your variable and this isn't possible because then your value would have nil which also isn't possible because your variable isn't optional. Then in this case make your variable optional

private var _myVar: Int?

Swift - class method which must be overridden by subclass

You have two options:

1. Use a Protocol

Define the superclass as a Protocol instead of a Class

Pro: Compile time check for if each "subclass" (not an actual subclass) implements the required method(s)

Con: The "superclass" (protocol) cannot implement methods or properties

2. Assert in the super version of the method

Example:

class SuperClass {
func someFunc() {
fatalError("Must Override")
}
}

class Subclass : SuperClass {
override func someFunc() {
}
}

Pro: Can implement methods and properties in superclass

Con: No compile time check



Related Topics



Leave a reply



Submit