컬렉션의 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 }
? :-)
본문을'return String (bytes : c.prefix (2), encoding : .utf8)! '으로 단순화 할 수 있습니다! –
확실합니다. 오류를 표시하는 단순화 된 예입니다. – Etan