Does _ArrayType or _ArrayProtocol not available in Swift 3.1?

前端 未结 1 393
谎友^
谎友^ 2021-01-27 00:26

I was using _ArrayType in my project when I was running on swift 2.1. I upgraded to swift 3.0.2 (Xcode 8.2.1) last week and I found here that _ArrayType

相关标签:
1条回答
  • 2021-01-27 00:41

    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.

    0 讨论(0)
提交回复
热议问题