Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why need to set public before override viewDidLoad in a public access control viewController

Tags:

public

ios

swift

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!

like image 262
Dennis Avatar asked Oct 15 '15 08:10

Dennis


People also ask

What is the purpose of viewDidLoad?

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.

Is viewDidLoad only called once?

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.


2 Answers

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.

like image 104
Steve Wilford Avatar answered Oct 07 '22 22:10

Steve Wilford


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

like image 1
katleta3000 Avatar answered Oct 08 '22 00:10

katleta3000