6

のプロトコルがありassociatedTypeます。typealiasプロトコル拡張でそのタイプのデフォルトを指定したいと思います。これは、特定のクラスから継承するクラスに対してのみ行われます。

protocol Foo: class {
  associatedtype Bar
  func fooFunction(bar: Bar)
}

プロトコル拡張:

extension Foo where Self: SomeClass {
  typealias Bar = Int
  func fooFunction(bar: Int) {
    // Implementation
  }
}

コンパイラはそれを不平を言い'Bar' is ambiguous for type lookup in this contextます。私も迅速な本で役立つものを見つけることができませんでした。

4

2 に答える 2

-1

拡張コンテキストで使用できる関連するバー型のバーが 2 つあり、コンパイラは正しいものを推測できません。これを試して。

protocol Foo: class {
  associatedtype Bar
  func fooFunction(bar: Bar)
}

extension Foo where Self: SomeClass {
  func fooFunction(bar: SomeClass.Bar) {}
}

class SomeClass:Foo{
  typealias Bar = Int
}
于 2016-05-28T09:22:56.897 に答える