将Collection的子序列转换为String

时间:2016-10-21 22:34:29

标签: swift generics collections sequence

我想将Collection的SubSequence转换为String。例如,此函数收集集合的前两个字节并将其转换为String。

func convert<T: Collection>(_ c: T) -> String 
    where T.Iterator.Element == UInt8 
{
    let start = c.startIndex
    let end = c.index(after: start)
    return String(bytes: c[start ... end], encoding: String.Encoding.utf8)!
}

这会导致此错误:

error: ambiguous reference to member 'subscript'
        return String(bytes: c[start ... end], encoding: String.Encoding.utf8)!
                             ~^~~~~~~~~~~~~~~
Swift.Collection:167:12: note: found this candidate
    public subscript(position: Self.Index) -> Self.Iterator.Element { get }
           ^
Swift.Collection:189:12: note: found this candidate
    public subscript(bounds: Range<Self.Index>) -> Self.SubSequence { get }
           ^
Swift.Collection:25:12: note: found this candidate
    public subscript(bounds: Range<Self.Index>) -> Slice<Self> { get }
           ^
Swift.IndexableBase:54:12: note: found this candidate
    public subscript(position: Self.Index) -> Self._Element { get }
           ^
Swift.IndexableBase:63:12: note: found this candidate
    public subscript(bounds: Range<Self.Index>) -> Self.SubSequence { get }
           ^
Swift.Indexable:23:12: note: found this candidate
    public subscript(bounds: ClosedRange<Self.Index>) -> Self.SubSequence { get }
           ^
Swift.Indexable:23:12: note: found this candidate
    public subscript(bounds: CountableRange<Self.Index>) -> Self.SubSequence { get }
           ^
Swift.Indexable:45:12: note: found this candidate
    public subscript(bounds: CountableClosedRange<Self.Index>) -> Self.SubSequence { get }

我在这里缺少什么? : - )

1 个答案:

答案 0 :(得分:3)

目前,Collection的{​​{1}}不能保证与集合本身具有相同的元素类型,这是由于SubSequence的限制。

事实上,SE-0142: Permit where clauses to constrain associated types的一个动机是允许约束associatedtype,这将强制实现这种关系。

虽然在这种特殊情况下,由于您首先没有使用associatedtype SubSequence : Sequence where SubSequence.Iterator.Element == Iterator.Element约束,因此您可以约束T.Iterator.Element == UInt8(感谢@MartinR):

T.SubSequence.Iterator.Element

(在一般情况下,您还需要func convert<T: Collection>(_ c: T) -> String where T.SubSequence.Iterator.Element == UInt8 { let start = c.startIndex let end = c.index(after: start) // please consider handling the case where String(bytes:encoding:) returns nil. return String(bytes: c[start ... end], encoding: String.Encoding.utf8)! } 约束到给定类型,您可能希望添加T.Iterator.Element作为附加约束。)