CollectionのSubSequenceをStringに変換したいと思っています。たとえば、この関数はコレクションの最初の2バイトを収集し、それをStringに変換します。Collectionのサブシーケンスを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 }
? :-)
本文を 'return String(bytes:c.prefix(2)、encoding:.utf8)! 'に簡略化することができます。 –
確かに。このエラーを示す簡単な例でした。 – Etan