Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 503
arun_K Avatar asked Oct 31 '17 11:10

arun_K


2 Answers

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

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.

like image 160
Krunal Avatar answered Nov 15 '22 13:11

Krunal


  1. 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.

  2. 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
    }
}
  1. If a variable is defined as private/fileprivate I am afraid you can not access them in an extension in another file.
like image 34
Puneet Sharma Avatar answered Nov 15 '22 13:11

Puneet Sharma