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

与世无争的帅哥 提交于 2019-12-08 16:02:54

问题


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.


回答1:


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.




回答2:


  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.


来源:https://stackoverflow.com/questions/47033934/accessing-fileprivate-and-private-variables-in-extension-and-another-class-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!