The class does not comply with the protocol. What for?

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
  }

}

I do not understand why. Any help was appreciated.

When you select the Xcode 'fix it' option, a stub for 'Fetcher is inserted. But Fetcher already has a type.

+4
source share
1 answer

This is now recognized as a bug in Swift 4. For now, we should avoid related types, limited to types that have common limitations.

So it's not cool

associatedtype U: Controller<T>

Removing this result results in the following:

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

Source: https://habr.com/ru/post/1688159/


All Articles