I have been going through the recent swift docs and working out on few examples in understanding private and fileprivate keywords in swift4. I am trying to access a fileprivate and private variable in an extension of the same class and another class subclassing the class but the output is unfruitful. I'm using in the following way
class privateUsageExample: UIViewController {
private var priVar = false
fileprivate var fPriVar = false
}
// usage of extension in the same class
extension privateUsageExample: UITextFieldDelegate {
if priVar{ // do something} // error : expected declaration
if fPriVar{ // do something} // error : expected declaration
func randFunc(){
self. fPriVar = true // accessible don't know the reason
}
}
// access of private and fileprivate variables in another class different file
class anotherUsageInDiffSwiftFile: privateUsageExample {
priVar = false // inaccessible (how to access it)
fPriVar = true // inaccessible (how to access it)
}
can you please help me out in accessing priVar (private) and fPriVar (fileprivate) variable in the extension of the same class in the same file and in another class subclassing the class in the different file.
In Swift 4.0, Private is now accessible in extension but within same file. If you declare/define extension in other file, then your private variable will not be accessible to your extension**
File Private
File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.
Syntax: fileprivate <var type> <variable name>
Example: fileprivate class SomeFilePrivateClass {}
Private
Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.
Syntax: private <var type> <variable name>
Example: private class SomePrivateClass {}
Here is more detail about all access levels: Swift - Access Levels
Look at this images:
File: ViewController.swift
Here extension and view controller both are in same file, hence private variable testPrivateAccessLevel
is accessible in extension
File: TestFile.swift
Here extension and view controller both are in different files, hence private variable testPrivateAccessLevel
is not accessible in extension.
Here class ViewController2
is a subclass of ViewController
and both are in same file. Here private variable testPrivateAccessLevel
is not accessible in Subclass but fileprivate is accessible in subclass.
You can not write if stubs inside an extension straightaway. It is meant to be written inside a function. So, that is not an issue with private/fileprivate but rather your understanding of extensions. Think of extensions as part of class declaration. In a class as well you can only write if stubs inside a func or closure.
The fileprivate variable inside an extension, was accessible even in Swift 3. So I don't understand, your comment //accessible don't know the reason
. Though, in Swift 4, private has changed its meaning in a sense that if the extension is defined at the same file as class, then the private vars are also accessible inside the extensions. This was not true in Swift3.
extension privateUsageExample: UITextFieldDelegate {
func randFunc(){
self.fPriVar = true
self.priVar = false
}
}
- If a variable is defined as private/fileprivate I am afraid you can not access them in an extension in another file.