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

3

18

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.

Wintery answered 31/10, 2017 at 11:9 Comment(0)
D
37

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.

Ditzel answered 31/10, 2017 at 11:44 Comment(3)
Swift has not yet full reflection support, and now this!Cardsharp
Seems private in Swift means that App is forced to copy entire library's code just to make a single variable public.Cardsharp
Based on my experience I am unable to access a private var from the class on an extension in a different file. This seems to be different what is specified in the TLDR. Can you please check, thanksExtirpate
D
4
  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.
Devour answered 31/10, 2017 at 11:29 Comment(2)
thanks for the reply. you have cleared most of my doubts but i haven't got the answer on accessing private/fileprivate variable inside the subclass of a class as asked in my question(last class of my question)Wintery
@ashwin: As I have mentioned, its not possible. You have to remove fileprivate/private if you want these variables accessible from another file.Devour
T
0

You can use the internal access level to access the variable in an extension that is in another file. Keep in mind it needs to be in the same module where the original class was defined.

Threw answered 25/3 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.