Accessing fileprivate and private variables in extension and another class using swift 4

arun_K picture arun_K · Oct 31, 2017 · Viewed 17.2k times · Source

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.

Answer

Krunal picture Krunal · Oct 31, 2017

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 `
*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

enter image description here


File: TestFile.swift
Here extension and view controller both are in different files, hence private variable testPrivateAccessLevel is not accessible in extension.

enter image description here

enter image description here


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.

enter image description here

TLDR: It is now not possible to define a property/function private or fileprivate for a class and then access it from an extension for that class in a different file. Access level private defined properties/functions are accessible in all classes and extensions defined in the same class, but not a subclass. Access level fileprivate defined properties/functions are accessible to extensions of class, subclasses of that class.