使用 swift 4 访问扩展名和另一个类中的文件私有和私有变量 [英] Accessing fileprivate and private variables in extension and another class using swift 4

查看:33
本文介绍了使用 swift 4 访问扩展名和另一个类中的文件私有和私有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览最近的 swift 文档,并研究了几个例子来理解 swift4 中的 private 和 fileprivate 关键字.我试图访问同一个类的扩展中的文件私有和私有变量,另一个类是该类的子类,但输出没有结果.我用以下方式

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)

 }

您能否帮助我访问同一文件中同一类的扩展名中的 priVar(私有)和 fPriVar(fileprivate)变量,以及在不同文件中子类化该类的另一个类中.

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.

推荐答案

在 Swift 4.0 中,Private 现在可以在扩展名中访问,但在同一文件中.如果您在其他文件中声明/定义扩展名,那么您的扩展名将无法访问您的私有变量**

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

文件私有
文件私有访问将实体的使用限制为它自己定义的源文件.当在整个文件中使用这些细节时,使用文件私有访问来隐藏特定功能的实现细节.
语法: fileprivate <变量名>
示例: fileprivate class SomeFilePrivateClass {}

私人
私有访问将实体的使用限制为封闭声明,以及同一文件中声明的扩展.当这些细节仅在单个声明中使用时,使用私有访问来隐藏特定功能的实现细节.
语法: private <变量名>
示例: 私有类 SomePrivateClass {}

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 {}


以下是有关所有访问级别的更多详细信息:Swift - 访问级别

看看这张图片:
文件: ViewController.swift
这里扩展和视图控制器都在同一个文件中,因此可以在扩展中访问私有变量testPrivateAccessLevel

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

文件: TestFile.swift
这里扩展和视图控制器都在不同的文件中,因此私有变量 testPrivateAccessLevel 在扩展中不可访问.

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


这里的 ViewController2 类是 ViewController 的子类,它们都在同一个文件中.这里私有变量 testPrivateAccessLevel 在子类中不可访问,但在子类中可访问 fileprivate.


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.

TLDR:现在无法为类定义属性/函数 privatefileprivate,然后从 不同的文件.访问级别private 定义的属性/函数可在同一类中定义的所有类和扩展中访问,但不能在子类中访问.访问级别 fileprivate 定义的属性/函数可被类的扩展、该类的子类访问.

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.

这篇关于使用 swift 4 访问扩展名和另一个类中的文件私有和私有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆