在Objective-C实例中,数据可以是公共的、受保护的或私有的。例如:

@interface Foo : NSObject
{
  @public
    int x;
  @protected:
    int y;
  @private:
    int z;
  }
-(int) apple;
-(int) pear;
-(int) banana;
@end

我没有发现任何提到的访问修饰符在Swift参考。是否有可能限制Swift中数据的可见性?


当前回答

据我所知,没有关键词“公共”,“私人”或“受保护”。这意味着一切都是公开的。

然而,苹果可能希望人们使用“协议”(世界上其他地方称为接口)和工厂设计模式来隐藏实现类型的细节。

无论如何,这通常是一个很好的设计模式;因为它允许您更改实现类层次结构,同时保持逻辑类型系统不变。

其他回答

till swift 2.0 there were only three access level [Public, internal, private] but in swift 3.0 apple added two new access level which are [ Open, fileType ] so now in swift 3.0 there are 5 access level Here I want to clear the role of these two access level 1. Open: this is much similar to Public but the only difference is that the Public can access the subclass and override, and Open access level can not access that this image is taken from Medium website and this describe the difference between open and public access

现在是第二个新的访问级别 2. 文件类型是更大版本的私有或更低的访问级别比内部 fileType可以访问[class, struct, enum]的扩展部分 而且private不能访问扩展部分的代码,它只能访问 词法作用域 这张照片取自Medium网站,描述了fileType和Private访问级别之间的区别

在Beta 6中,文档指出有三种不同的访问修饰符:

公共 内部 私人

这三点适用于类、协议、函数和属性。

public var somePublicVariable = 0
internal let someInternalConstant = 0
private func somePrivateFunction() {}

有关更多信息,请检查访问控制。

从Swift 3.0.1开始,共有4个访问级别,从最高(限制最少)到最低(限制最多)。


1. 公开和公共

允许在定义模块(目标)之外使用一个实体。在指定框架的公共接口时,通常使用开放或公共访问。

然而,开放访问仅适用于类和类成员,它与公共访问的区别如下:

公共类和类成员只能在定义模块(目标)内进行子类化和重写。 开放类和类成员可以在定义模块(目标)内外被子类化和重写。

// First.framework – A.swift

open class A {}

// First.framework – B.swift

public class B: A {} // ok

// Second.framework – C.swift

import First

internal class C: A {} // ok

// Second.framework – D.swift

import First

internal class D: B {} // error: B cannot be subclassed

2. 内部

启用在定义模块(目标)中使用的实体。在定义应用程序或框架的内部结构时,通常使用内部访问。

// First.framework – A.swift

internal struct A {}

// First.framework – B.swift

A() // ok

// Second.framework – C.swift

import First

A() // error: A is unavailable

3.fileprivate

将实体的使用限制到其定义的源文件。当在整个文件中使用特定功能块的实现细节时,通常使用文件私有访问来隐藏这些细节。

// First.framework – A.swift

internal struct A {

    fileprivate static let x: Int

}

A.x // ok

// First.framework – B.swift

A.x // error: x is not available

4. 私人

将实体的使用限制在其封闭声明范围内。当只在单个声明中使用特定功能块的实现细节时,通常使用私有访问来隐藏这些细节。

// First.framework – A.swift

internal struct A {

    private static let x: Int

    internal static func doSomethingWithX() {
        x // ok
    }

}

A.x // error: x is unavailable

希望为那些想要类似保护方法的人节省一些时间:

与其他答案一样,swift现在提供了“private”修饰符——它是按文件定义的,而不是像Java或c#那样按类定义的。这意味着如果你想要受保护的方法,如果它们在同一个文件中,你可以使用swift私有方法

创建一个基类来保存“受保护的”方法(实际上是私有的) 子类化这个类以使用相同的方法 在其他文件中,您不能访问基类方法,即使您创建了子类

文件1:

class BaseClass {
    private func protectedMethod() {
        
    }
}

class SubClass : BaseClass {
    func publicMethod() {
        self.protectedMethod()  //this is ok as they are in same file
    }
}

文件2:

func test() {
    var a = BaseClass()
    a.protectedMethod() //ERROR


    var b = SubClass()
    b.protectedMethod() //ERROR
}

class SubClass2 : BaseClass {
    func publicMethod() {
        self.protectedMethod() //ERROR
    }
}

据我所知,没有关键词“公共”,“私人”或“受保护”。这意味着一切都是公开的。

然而,苹果可能希望人们使用“协议”(世界上其他地方称为接口)和工厂设计模式来隐藏实现类型的细节。

无论如何,这通常是一个很好的设计模式;因为它允许您更改实现类层次结构,同时保持逻辑类型系统不变。