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(){}
}
Technically, the difference is that the private
extension
makes the methods fileprivate
, not private
.
But the real question is why would one use a private
extension
rather than just declaring the individual methods as private
?
We do this because:
private
avoids the syntactic noise of repeating private
keyword for each method.So, private
methods and methods inside a private
extension
are not technically the same thing, but the difference is subtle and the private
extension
pattern is, nonetheless, extremely convenient. It concisely designates that none of the methods contained within are used outside of the current file.
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