MutableCollection
に準拠する方法に関するドキュメントが見つかりません。 Googleはその話題についてはまったく空白です。MutableCollectionに準拠させるには?
例は、私がGMSPath
/GMSMutablePath
のための適合性を追加したい:MutableCollection
状態の
import CoreLocation
import GoogleMaps
extension GMSPath: RandomAccessCollection {
public var startIndex: Int {
return 0
}
public var endIndex: Int {
return count
}
public func index(before i: Int) -> Int {
return i-1
}
public func index(after i: Int) -> Int {
return i+1
}
public subscript(position: Int) -> CLLocationCoordinate2D {
return coordinate(at: UInt(position))
}
}
extension GMSMutablePath: MutableCollection { // Error!
public override subscript(position: Int) -> CLLocationCoordinate2D {
get {
return coordinate(at: UInt(position))
}
set {
replaceCoordinate(at: UInt(position), with: newValue)
}
}
}
Error: Type 'GMSMutablePath' does not conform to a) 'MutableIndexable', b) 'MutableCollection'.
ドキュメント:
To add conformance to the MutableCollection protocol to your own custom collection, upgrade your type’s subscript to support both read and write access.
私はそれをしました。 MutableIndexable
から
MutableCollection
継承、述べドキュメント:
In most cases, it’s best to ignore this protocol and use the MutableCollection protocol instead, because it has a more complete interface.
フムを?
MutableCollectionはMutableIndexableのサブタイプなので、MutableCollectionに付与するにはMutableIndexableに準拠する必要があります – Alexander
正確に実装するためのヒントはありますか?デフォルトの実装などでは、実装する必要があることを決して知りません。そして、ドキュメントはまったく明確ではありません。 – tombardey
デフォルトの実装は、少なくともすべてを実装する必要があるドキュメントにリストされていますが、 – Alexander