Why need to set public before override viewDidload in a public access control viewController
public class customViewController: UIViewController {
override public func viewDidLoad() {
super.viewDidLoad()
}
}
if I remove the public, Xcode will give an error warning!
viewDidLoad( ) allows you to initialize properties of the view/viewController object and finalize them before viewWillAppear( ) is called. So, which method is loaded first? viewDidLoad( ) is called when the view has finished loading, while loadView( ) is called when the view starts loading.
viewDidLoad() is one of the initialization methods that is called on the initial view controller. viewDidLoad() is called before anything is shown to the user - and it is called only once.
The error message is fairly explicit:
Overriding instance method must be as accessible as the declaration it overrides.
This means that a method must not have a lower access level than the method it overrides.
For example given this class:
public class Superclass {
internal func doSomething() {
...
}
}
You cannot then override doSomething
with a method that is less accessible than interal
. e.g.
public class Subclass : Superclass {
// error
private override func doSomething() {
}
}
You can however override a method and make it more accessible:
public class Subclass : Superclass {
public override func doSomething() {
// You can even call the internal method in the superclass
super.doSomething()
}
}
The reference documentation has lots of detail on this, but seems to leave this relationship to implication.
Took from here
Public
access enables entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use public access when specifying the public interface to a framework.
Internal
access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.
Private
access restricts the use of an entity to the enclosing declaration. Use private access to hide the implementation details of a specific piece of functionality.
File-private
access restricts the use of an entity to its own defining source file.
Do you need public modifier? You can write it like this:
class customViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
No warnings
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With