在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 4 / Swift 5
正如Swift文档-访问控制中提到的,Swift有5个访问控制:
开放和公共:可以从它们的模块实体和导入定义模块的任何模块实体中访问。 Internal:只能从其模块的实体中访问。这是默认的访问级别。 文件私有和私有:只能在您定义的有限范围内进行有限访问。
open和public的区别是什么?
open和之前版本的Swift中的public是一样的,它们允许来自其他模块的类使用和继承它们,即:它们可以从其他模块继承子类。此外,它们允许来自其他模块的成员使用和重写它们。同样的逻辑也适用于它们的模块。
Public允许来自其他模块的类使用它们,但不能继承它们,即:它们不能从其他模块子类化。此外,它们允许来自其他模块的成员使用它们,但不允许重写它们。对于它们的模块,它们具有相同的open's逻辑(它们允许类使用和继承它们;它们允许成员使用和重写它们)。
文件私有和私有的区别是什么?
私有文件可以从他们的整个文件访问。
Private只能从它们的单个声明和同一文件中该声明的扩展进行访问;例如:
// Declaring "A" class that has the two types of "private" and "fileprivate":
class A {
private var aPrivate: String?
fileprivate var aFileprivate: String?
func accessMySelf() {
// this works fine
self.aPrivate = ""
self.aFileprivate = ""
}
}
// Declaring "B" for checking the abiltiy of accessing "A" class:
class B {
func accessA() {
// create an instance of "A" class
let aObject = A()
// Error! this is NOT accessable...
aObject.aPrivate = "I CANNOT set a value for it!"
// this works fine
aObject.aFileprivate = "I CAN set a value for it!"
}
}
Swift 3和Swift 4的访问控制有什么不同?
正如在SE-0169提案中提到的,Swift 4唯一的改进是私有访问控制范围被扩展,可以从同一文件中的声明扩展中访问;例如:
struct MyStruct {
private let myMessage = "Hello World"
}
extension MyStruct {
func printMyMessage() {
print(myMessage)
// In Swift 3, you will get a compile time error:
// error: 'myMessage' is inaccessible due to 'private' protection level
// In Swift 4 it should works fine!
}
}
因此,不需要将myMessage声明为文件私有以在整个文件中可访问。
其他回答
Xcode 6中引入的访问控制机制:
Swift provides three different access levels for entities within your code. These access levels are relative to the source file in which an entity is defined, and also relative to the module that source file belongs to. Public access enables 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 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. Private access restricts the use of an entity to its own defining source file. Use private access to hide the implementation details of a specific piece of functionality. Public access is the highest (least restrictive) access level and private access is the lowest (or most restrictive) access level.
默认在内部访问,因此不需要指定。还要注意,私有说明符不是在类级别上工作,而是在源文件级别上工作。这意味着要使类的某些部分真正私有,您需要将其分离到自己的文件中。这也介绍了一些关于单元测试的有趣案例……
我提出的另一个观点(在上面的链接中有评论)是,你不能“升级”访问级别。如果你子类化了某个东西,你可以对它进行更多的限制,但反过来就不行。
最后一点也会影响函数、元组和其他东西,例如,如果一个函数使用了私有类,那么将函数设置为内部或公共是无效的,因为它们可能无法访问私有类。这将导致编译器警告,并且您需要将该函数重新声明为私有函数。
从Xcode 6 beta 4开始,Swift就有了访问修饰符。发布说明如下:
Swift访问控制分为三种访问级别: 私有实体只能从定义它们的源文件中访问。 内部实体可以在目标中定义它们的任何地方访问。 公共实体可以从目标中的任何地方访问,也可以从导入当前目标模块的任何其他上下文访问。
隐式默认值是内部的,因此在应用程序目标中,您可以关闭访问修饰符,除非在需要更严格限制的地方。在框架目标中(例如,如果你嵌入一个框架来在应用程序和共享或Today视图扩展之间共享代码),使用public指定你想要向框架的客户端公开的API。
在Beta 6中,文档指出有三种不同的访问修饰符:
公共 内部 私人
这三点适用于类、协议、函数和属性。
public var somePublicVariable = 0
internal let someInternalConstant = 0
private func somePrivateFunction() {}
有关更多信息,请检查访问控制。
Swift 4 / Swift 5
正如Swift文档-访问控制中提到的,Swift有5个访问控制:
开放和公共:可以从它们的模块实体和导入定义模块的任何模块实体中访问。 Internal:只能从其模块的实体中访问。这是默认的访问级别。 文件私有和私有:只能在您定义的有限范围内进行有限访问。
open和public的区别是什么?
open和之前版本的Swift中的public是一样的,它们允许来自其他模块的类使用和继承它们,即:它们可以从其他模块继承子类。此外,它们允许来自其他模块的成员使用和重写它们。同样的逻辑也适用于它们的模块。
Public允许来自其他模块的类使用它们,但不能继承它们,即:它们不能从其他模块子类化。此外,它们允许来自其他模块的成员使用它们,但不允许重写它们。对于它们的模块,它们具有相同的open's逻辑(它们允许类使用和继承它们;它们允许成员使用和重写它们)。
文件私有和私有的区别是什么?
私有文件可以从他们的整个文件访问。
Private只能从它们的单个声明和同一文件中该声明的扩展进行访问;例如:
// Declaring "A" class that has the two types of "private" and "fileprivate":
class A {
private var aPrivate: String?
fileprivate var aFileprivate: String?
func accessMySelf() {
// this works fine
self.aPrivate = ""
self.aFileprivate = ""
}
}
// Declaring "B" for checking the abiltiy of accessing "A" class:
class B {
func accessA() {
// create an instance of "A" class
let aObject = A()
// Error! this is NOT accessable...
aObject.aPrivate = "I CANNOT set a value for it!"
// this works fine
aObject.aFileprivate = "I CAN set a value for it!"
}
}
Swift 3和Swift 4的访问控制有什么不同?
正如在SE-0169提案中提到的,Swift 4唯一的改进是私有访问控制范围被扩展,可以从同一文件中的声明扩展中访问;例如:
struct MyStruct {
private let myMessage = "Hello World"
}
extension MyStruct {
func printMyMessage() {
print(myMessage)
// In Swift 3, you will get a compile time error:
// error: 'myMessage' is inaccessible due to 'private' protection level
// In Swift 4 it should works fine!
}
}
因此,不需要将myMessage声明为文件私有以在整个文件中可访问。
据我所知,没有关键词“公共”,“私人”或“受保护”。这意味着一切都是公开的。
然而,苹果可能希望人们使用“协议”(世界上其他地方称为接口)和工厂设计模式来隐藏实现类型的细节。
无论如何,这通常是一个很好的设计模式;因为它允许您更改实现类层次结构,同时保持逻辑类型系统不变。