在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中的信息。它不是很干净,也不是很好读,但它确实有用。
例子:
protocol HuhThing {
var huh: Int { get set }
}
func HuhMaker() -> HuhThing {
class InnerHuh: HuhThing {
var innerVal: Int = 0
var huh: Int {
get {
return mysteriousMath(innerVal)
}
set {
innerVal = newValue / 2
}
}
func mysteriousMath(number: Int) -> Int {
return number * 3 + 2
}
}
return InnerHuh()
}
HuhMaker()
var h = HuhMaker()
h.huh // 2
h.huh = 32
h.huh // 50
h.huh = 39
h.huh // 59
innerVal和mysterousmath隐藏在这里,不被外部使用,试图挖掘对象的方法应该会导致错误。
我只是阅读了Swift文档的一部分,所以如果这里有缺陷,请指出来,我很想知道。
其他回答
现在在测试版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.
对于Swift 1-3:
不,不可能。这里根本没有任何私有/受保护的方法和变量。
一切都是公开的。
更新 从Swift 4开始,可能会在这个帖子中看到其他答案
从Xcode 6 beta 4开始,Swift就有了访问修饰符。发布说明如下:
Swift访问控制分为三种访问级别: 私有实体只能从定义它们的源文件中访问。 内部实体可以在目标中定义它们的任何地方访问。 公共实体可以从目标中的任何地方访问,也可以从导入当前目标模块的任何其他上下文访问。
隐式默认值是内部的,因此在应用程序目标中,您可以关闭访问修饰符,除非在需要更严格限制的地方。在框架目标中(例如,如果你嵌入一个框架来在应用程序和共享或Today视图扩展之间共享代码),使用public指定你想要向框架的客户端公开的API。
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访问级别之间的区别
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声明为文件私有以在整个文件中可访问。