类不符合协议。为什么呢?

时间:2017-10-25 13:40:24

标签: swift syntax protocols

class Controller<U: NSObject> {}

protocol Robert {
  associatedtype T
  associatedtype U: Controller<T>
  var fetcher: U { get }
}

class Telephone: NSObject {}

class Object: Telephone {}

class Turtle: Controller<Object> {}

class Fish: Robert {

  typealias T = Object
  typealias U = Turtle

  let x = Turtle()

  var fetcher: Turtle {
    return x
  }

}

我不明白为什么。任何帮助表示赞赏。

选择XCode“修复它”选项时,会插入“Fetcher”的存根。但是Fetcher已经有了一个类型。

1 个答案:

答案 0 :(得分:1)

现在已经在Swift 4中认识到as a bug。现在我们必须避免受具有通用约束的类型约束的关联类型。

所以这不酷#/ p>

associatedtype U: Controller<T>

删除它会产生以下效果。

protocol Robert {
  associatedtype T: NSObject
  var fetcher: Controller<T> { get }
}