当前位置: 动力学知识库 > 问答 > 编程问答 >

In swift, when would explicitly using the access modifier "internal", while declaring a class, have any effect?

问题描述:

In swift, when would explicitly using the access modifier "internal", while declaring a class, have any effect ?

网友答案:

It won't have an effect on the compiler, since it's the default (but see below). But it can have an effect on developers who read the code, by signaling the intent. For example, consider a nested type:

public struct X {
    struct Y {
         init() {}
    }
}

A casual reader may believe that Y is public since X is. But this is not the case. Y is internal. Adding an explicit internal to the definition can make it clear that the missing public was intentional rather than an oversight.

Currently it is possible to create misleading situations with internal. For example:

private struct X {
    internal struct Y {
         init() {}
    }
}

One might expect this to be an error (or at least a warning), but it is not. This is actually intended behavior (described by SE-0025). I recommend SE-0025 if you want to learn more about the current state of access controls, since they changed in Swift 3.

网友答案:

Use internal if you want the method / property to be only accessible within the current module, not outside. From Apple's Language Guide:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: