2016-10-17 6 views
3

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.

フムを?

+0

MutableCollectionはMutableIndexableのサブタイプなので、MutableCollectionに付与するにはMutableIndexableに準拠する必要があります – Alexander

+0

正確に実装するためのヒントはありますか?デフォルトの実装などでは、実装する必要があることを決して知りません。そして、ドキュメントはまったく明確ではありません。 – tombardey

+0

デフォルトの実装は、少なくともすべてを実装する必要があるドキュメントにリストされていますが、 – Alexander

答えて

1

ここでの問題は、私がRandomAccessCollectionにも適合していたことです。ちょうどCollection/MutableCollectionの代わりに問題になりました。その場合、ドキュメントが約束するものとは反対に、添字setterを提供するだけではありません。具体的には、スライスの下付き文字を実装する必要があります。

私は次のようになりました。 typaliasesは、コンパイラが常にそれらを推論しているように見えないので必要です。スウィフトの条件付デフォルトのプロトコルの実装と

extension GMSPath: RandomAccessCollection { 

    public typealias Index = Int 
    public typealias Indices = DefaultRandomAccessIndices<GMSPath> 

    public var startIndex: Index { 
     return 0 
    } 

    public var endIndex: Index { 
     return count 
    } 

    public func index(before i: Index) -> Index { 
     return i-1 
    } 

    public func index(after i: Index) -> Index { 
     return i+1 
    } 

    public subscript(position: Index) -> CLLocationCoordinate2D { 
     return coordinate(at: UInt(position)) 
    } 
} 

extension GMSMutablePath: MutableCollection { 

    public subscript(bounds: Range<Index>) -> RandomAccessSlice<GMSPath> { 
     get { return .init(base: self, bounds: bounds) } 
     set { 
      assert(newValue.count == bounds.count) 
      newValue.enumerated().forEach { self[$0] = $1 } 
     } 
    } 

    public override subscript(position: Index) -> CLLocationCoordinate2D { 
     get { return coordinate(at: UInt(position)) } 
     set { replaceCoordinate(at: UInt(position), with: newValue) } 
    } 
} 

、私はそれは本当にハードに正確に実装する必要があるかを調べるために見つけます。

関連する問題