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:
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.