[英]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. 但是,即使我将privateFoo()声明为private
,我仍然可以从扩展名外部调用它,这不是我所期望的。
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. 我认为你可以把你的private extension
放在其他文件中,它会做到这一点。
class A { }
FileB.swift FileB.swift
private extension A { func foo() { privateFoo() } func privateFoo() { } }
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.