在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中数据的可见性?
当前回答
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中的信息。它不是很干净,也不是很好读,但它确实有用。
例子:
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 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编程语言(访问控制)
从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
据我所知,没有关键词“公共”,“私人”或“受保护”。这意味着一切都是公开的。
然而,苹果可能希望人们使用“协议”(世界上其他地方称为接口)和工厂设计模式来隐藏实现类型的细节。
无论如何,这通常是一个很好的设计模式;因为它允许您更改实现类层次结构,同时保持逻辑类型系统不变。