简体   繁体   中英

Swift 4 - Private method inside extension

I'm looking for a way to write a private function inside an extension.

For example:

class A: UIViewController {
    override viewDidLoad() {
        privateFoo()
    }
}

private extension A {
    func foo() { 
        privateFoo()
    }

    private func privateFoo() { //Helper function for foo(), expected to be called inside the scope of this extension only

    }
}

However, even if I declare privateFoo() as private , I can still call it from outside of the extension, which is not what I expected it to be.

Can you someone please help me how to achieve my goal?

Sorry I don't have enough reputation to reply.

I think you can put your private extension in other file, it will do the trick.


For example:

FileA.swift

 class A { } 

FileB.swift

 private extension A { func foo() { privateFoo() } func privateFoo() { } } 

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM