在Objective-C实例中,数据可以是公共的、受保护的或私有的。例如:
@interface Foo : NSObject
{
@public
int x;
@protected:
int y;
@private:
int z;
}
-(int) apple;
-(int) pear;
-(int) banana;
@end
我没有发现任何提到的访问修饰符在Swift参考。是否有可能限制Swift中数据的可见性?
在Objective-C实例中,数据可以是公共的、受保护的或私有的。例如:
@interface Foo : NSObject
{
@public
int x;
@protected:
int y;
@private:
int z;
}
-(int) apple;
-(int) pear;
-(int) banana;
@end
我没有发现任何提到的访问修饰符在Swift参考。是否有可能限制Swift中数据的可见性?
当前回答
Swift 3.0提供了5种不同的访问控制:
开放 公共 内部 fileprivate 私人
Open access and public access enable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use open or public access when specifying the public interface to a framework. Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure. 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. Private access restricts the use of an entity to the enclosing declaration. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration. Open access is the highest (least restrictive) access level and private access is the lowest (most restrictive) access level.
默认访问级别
如果您自己没有明确指定访问级别,那么代码中的所有实体(除了少数特定的例外)都有一个默认的内部访问级别。因此,在许多情况下,您不需要在代码中指定显式的访问级别。
关于该主题的发布说明:
Classes declared as public can no longer be subclassed outside of their defining module, and methods declared as public can no longer be overridden outside of their defining module. To allow a class to be externally subclassed or a method to be externally overridden, declare them as open, which is a new access level beyond public. Imported Objective-C classes and methods are now all imported as open rather than public. Unit tests that import a module using an @testable import will still be allowed to subclass public or internal classes as well as override public or internal methods. (SE-0117)
更多信息和细节: Swift编程语言(访问控制)
其他回答
你可以使用的一个选项是将实例创建包装到一个函数中,并在构造函数中提供适当的getter和setter:
class Counter {
let inc: () -> Int
let dec: () -> Int
init(start: Int) {
var n = start
inc = { ++n }
dec = { --n }
}
}
let c = Counter(start: 10)
c.inc() // 11
c.inc() // 12
c.dec() // 11
在Beta 6中,文档指出有三种不同的访问修饰符:
公共 内部 私人
这三点适用于类、协议、函数和属性。
public var somePublicVariable = 0
internal let someInternalConstant = 0
private func somePrivateFunction() {}
有关更多信息,请检查访问控制。
从Xcode 6 beta 4开始,Swift就有了访问修饰符。发布说明如下:
Swift访问控制分为三种访问级别: 私有实体只能从定义它们的源文件中访问。 内部实体可以在目标中定义它们的任何地方访问。 公共实体可以从目标中的任何地方访问,也可以从导入当前目标模块的任何其他上下文访问。
隐式默认值是内部的,因此在应用程序目标中,您可以关闭访问修饰符,除非在需要更严格限制的地方。在框架目标中(例如,如果你嵌入一个框架来在应用程序和共享或Today视图扩展之间共享代码),使用public指定你想要向框架的客户端公开的API。
现在在测试版4中,他们为Swift添加了访问修饰符。
from Xcode 6 beta 4 realese notes:
Swift access control has three access levels: private entities can only be accessed from within the source file where they are defined. internal entities can be accessed anywhere within the target where they are defined. public entities can be accessed from anywhere within the target and from any other context that imports the current target’s module. By default, most entities in a source file have internal access. This allows application developers to largely ignore access control while allowing framework developers full control over a framework's API.
语言语法没有关键字“public”,“private”或“protected”。这意味着一切都是公开的。当然,可以有一些替代方法来指定没有这些关键字的访问修饰符,但我在语言参考中找不到它。