Swift 3.1中不提供_ArrayType或_ArrayProtocol吗?

问题描述:

当我在swift 2.1上运行时,我在我的项目中使用了_ArrayType。我上周升级到swift 3.0.2(Xcode 8.2.1),我发现here_ArrayType更改为_ArrayProtocol,它运行良好。Swift 3.1中不提供_ArrayType或_ArrayProtocol吗?

今天我升级了我的Xcode到8.3.1,它给了我错误: Use of undeclared type '_ArrayProtocol'。这里是我的代码:

extension _ArrayProtocol where Iterator.Element == UInt8 { 
    static func stringValue(_ array: [UInt8]) -> String { 
     return String(cString: array) 
    } 
} 

现在有什么问题?为什么_ArrayProtocol在swift 3.0.2中工作时在swift 3.1中未声明。

另外,当我看这里in git我看_ArrayProtocol可用。 比我看了Swift 2.1 docs我能看到'_ArrayType'在协议列表中,但在斯威夫特3.0/3.1文档我无法看到_ArrayProtocol

+0

相关http://*.com/questions/40691327/cant-assign-the-item-in-arrayprotocol –

以下划线开头的类型名称应始终视为内部。 在Swift 3.1中,源代码中标记为internal,因此 不公开可见。

使用_ArrayProtocol解决办法在早期版本的雨燕在那里 你不能定义一个Array扩展了“同一类型”的规定。 这是现在可以作为雨燕3.1,如 Xcode 8.3 release notes描述:

Constrained extensions allow same-type constraints between generic parameters and concrete types. (SR-1009)

使用,因此内部协议不再是必需的, ,你可以简单地定义

extension Array where Element == UInt8 { 

} 

但是请注意,您的static func stringValue()不需要任何 元素类型的限制。你或许意欲是 定义实例方法这样的:

extension Array where Element == UInt8 { 

    func stringValue() -> String { 
     return String(cString: self) 
    } 

} 

print([65, 66, 67, 0].stringValue()) // ABC 

还要注意的是String(cString:)需要一个空值终止的UTF-8字节序列 。