Does _ArrayType or _ArrayProtocol not available in Swift 3.1?

懵懂的女人 提交于 2019-12-02 03:19:52

Type names starting with an underscore should always treated as internal. In Swift 3.1, it is marked as internal in the source code and therefore not publicly visible.

Using _ArrayProtocol was a workaround in earlier Swift versions where you could not define an Array extension with a "same type" requirement. This is now possible as of Swift 3.1, as described in the Xcode 8.3 release notes:

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

Using the internal protocol is therefore not necessary anymore, and you can simply define

extension Array where Element == UInt8 {

}

But note that your static func stringValue() does not need any restriction of the element type. What you perhaps intended is to define an instance method like this:

extension Array where Element == UInt8 {

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

}

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

Also note that String(cString:) expects a null-terminated sequence of UTF-8 bytes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!