12

在 Swift 4 中,由于 nowprivate在同一个源代码文件中的扩展中也是可见的,它与fileprivate访问修饰符有何不同?

背景:在 Swift 3 中,类中的私有变量在同一个文件的扩展中是不可见的。为此,fileprivate不得不使用。

4

7 回答 7

38

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


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


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

回答您的问题:( 在 Swift 3 中,类中的私有变量在同一文件的扩展中不可见。为此,必须使用 fileprivate。)

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


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

在此处输入图像描述


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

在此处输入图像描述

在此处输入图像描述


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

在此处输入图像描述

于 2017-10-05T15:01:27.150 回答
9

适用于 swift 4.0 及其版本
Private
Private 仅在类及其扩展名中访问(当扩展名在同一个 .swift 文件中时)。

文件私有
文件-仅在类及其扩展名和子类中的私有访问(当扩展名或子类在同一个 .swift 文件中时)。

于 2018-09-18T11:32:00.943 回答
1
///////////////ViewController1.swift file
        class ViewController1 {
            private func testPrivate() {
                print("testPrivate")
            }
            fileprivate func testFilePrivate() {
                print("testFilePrivate")
            }
            func doesNothing1() {
                testPrivate() //success
                testFilePrivate() //success
            }
        }
        extension ViewController1 {
            func doesNothingInExtensionSameFile() {
                testPrivate() //success
                testFilePrivate() //success
            }
        }
        class SomeOtherClassInSameFile {
            let vc1 = ViewController1()
            func doesNothing() {
               vc1.testPrivate() //throws error
               vc1.testFilePrivate() //success
            } 
        }
////////////// ViewController2.swift file
        extension ViewController1 {
            func doesNothingInExtensionDifferentFile() {
                testPrivate() //throws error
                testFilePrivate() //throws error
            }
        }
于 2019-02-12T15:11:04.673 回答
0

公开与公开:

  • Public 不允许在另一个模块/目标中继承类,而 Open 允许。
  • 公共方法不允许在另一个模块/目标的子类中被覆盖,而 Open 允许。

除上述之外,两者都是相同的。

私有与文件私有:

  • (在单个文件中)Private 不允许访问子类中的(func 和属性),而 FilePrivate 允许。
  • (外部文件)Private 和 FilePrivate 都无法访问。

除上述之外,两者都是相同的。

于 2020-10-19T04:55:14.703 回答
0

private and fileprivate access levels have come closer with Swift4.

The difference in access lies as follows:

fileprivate members - only & entirely within that .swift file

private members - only in that class & extension of the class if both are present in same .swift file

Hence only fileprivate members(not private) can be accessed in

  • Sub Classes in the same .swift file
  • Instances of the class (initialized in another class) in the same .swift file.
于 2019-07-29T12:21:03.180 回答
-2

“Private”只能在类中访问,“FilePrivate”只能在 .swift 文件中访问。

于 2018-09-18T07:16:13.290 回答
-3

Private:类和类扩展中的访问。FilePrivate : 在类、子类、扩展中访问,

于 2019-07-28T12:23:11.497 回答