Swift - 'internal' 修饰符不能在协议中使用
Swift - 'internal' modifier cannot be used in protocols
当我在 Swift 协议内部声明函数时出现编译器错误,
protocol MyProtocol {
internal func test()
}
错误:
'internal' modifier cannot be used in protocols
如果我删除访问修饰符 internal
,错误会得到解决,但我想知道为什么会出现此错误?正如 documentation 提到的,如果我没有明确提及任何访问级别,则默认访问级别将是内部的。那么为什么当我明确表示时它会产生编译器错误。
请在链接文章中向下滚动
Protocols
If you want to assign an explicit access level to a protocol type, do so at the point that you define the protocol. This enables you to create protocols that can only be adopted within a certain access context.
The access level of each requirement within a protocol definition is automatically set to the same access level as the protocol. You can’t set a protocol requirement to a different access level than the protocol it supports. This ensures that all of the protocol’s requirements will be visible on any type that adopts the protocol.
这意味着你必须申报
internal protocol MyProtocol {
func test()
}
当我在 Swift 协议内部声明函数时出现编译器错误,
protocol MyProtocol {
internal func test()
}
错误:
'internal' modifier cannot be used in protocols
如果我删除访问修饰符 internal
,错误会得到解决,但我想知道为什么会出现此错误?正如 documentation 提到的,如果我没有明确提及任何访问级别,则默认访问级别将是内部的。那么为什么当我明确表示时它会产生编译器错误。
请在链接文章中向下滚动
Protocols
If you want to assign an explicit access level to a protocol type, do so at the point that you define the protocol. This enables you to create protocols that can only be adopted within a certain access context.
The access level of each requirement within a protocol definition is automatically set to the same access level as the protocol. You can’t set a protocol requirement to a different access level than the protocol it supports. This ensures that all of the protocol’s requirements will be visible on any type that adopts the protocol.
这意味着你必须申报
internal protocol MyProtocol {
func test()
}