2017-04-11 54 views
1

當我在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://stackoverflow.com/questions/40691327/cant-assign-the-item-in-arrayprotocol –

回答

2

以下劃線開頭的類型名稱應始終視爲內部。 在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字節序列 。