Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a difference when you create a private func vs creating a func in a private extension?

Can I create private functions by putting my functions inside of a private extension of the class instead of creating new private functions by constantly calling private func functionName(){}?

Doing this:

private extension mClass {
    func mFuncOne(){}
    func mFuncTwo(){}
    func mFuncThree(){}
    func mFuncFour(){}
    func mFuncFive(){}
}

instead of:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class mClass: {

    private func mFuncOne(){}
    private func mFuncTwo(){}
    private func mFuncThree(){}
    private func mFuncFour(){}
    private func mFuncFive(){}
}

>Solution :

Yes you can do this. It’s just that the extension and the original declaration has to be in the same file. According to the Swift Guide:

Extensions that are in the same file as the class, structure, or enumeration that they extend behave as if the code in the extension had been written as part of the original type’s declaration. As a result, you can:

  • Declare a private member in an extension, and access that member from the original declaration in the same file.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading