Я хотел бы преобразовать SubSequence коллекции в строку. Например, эта функция собирает первые два байта коллекции и преобразует ее в строку.Преобразование подпоследовательности коллекции в строку
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 }
Что я здесь отсутствует? :-)
Вы можете упростить тело, чтобы вернуть String (байты: c.prefix (2), encoding: .utf8)! ' –
Конечно. Это был упрощенный пример, чтобы показать ошибку. – Etan