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

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

问题描述

我一直在浏览最近的swift文档,并研究了一些示例来了解swift4中的private和fileprivate关键字。我试图在同一个类的扩展名中访问fileprivate和private变量,而另一个类对该类进行了子类化,但是输出没有成果。我以以下方式使用

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(文件私有)变量。同一文件的同一类的扩展名,以及在另一个文件中将其子类化的另一个类的扩展名。

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< var type> <变量名>

示例: fileprivate class SomeFilePrivateClass {}



私有

私有访问将实体的使用限制为封闭声明和扩展相同文件中的声明。仅在单个声明中使用特定细节的实现细节时,使用私有访问权限可以隐藏这些细节。

语法: private< var type> <变量名>

示例: 私有类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 {}


有关所有访问级别的详细信息,请参见:快捷键-访问级别

看看这张图片:
< br> 文件: 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.

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

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