Reputation: 86
Is there a way to make the extension able to access private var defined in the definition file, but keep the extension's private function private to that extension only (not assessable in definition file)?
For example:
class MyClass {
private var myStr = "str"
func doSomething() {
funcA() // <- should cause compiler error
}
}
extension MyClass {
private func funcA() {
print(myStr)
}
}
So doSomething() can not call funcA(), but funcA() can access myStr.
Upvotes: 1
Views: 223
Reputation:
Yes. Put the definition in one file, and the extension in another.
Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file.
That information is not quite complete. As you've seen, anything private
in an extension is accessible in the declaration, and other extensions. But only in the same file.
https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html
Upvotes: 1