0

I'm trying to implement a code with this structure

   protocol MyClassProtocol: class {
   }

   protocol MyOtherProtocol {
        typealias MI: MyClassProtocol

        weak var interface: MI? {get set}
    }

    protocol MySpecializedClassProtocol: MyClassProtocol {
        // Other methods
    }

    struct MyStruct: MySpecializedClassProtocol {
        typealias MI = MySpecializedClassProtocol

        weak var interface: MySpecializedClassProtocol?
    }

I get an error

"possibly intended match 'MI' (aka 'MyClassProtocol') does not conform to 'MyClassProtocol' typealias MI = MyClassProtocol"

So I'm doing something wrong. But I don't understand what. How can I see in swift that MI should adopt MyClassProtocol?

Luca Bartoletti
  • 2,435
  • 1
  • 24
  • 46
  • 1
    You've restricted your `MyClassProtocol` only to be used on classes. – Mtoklitz113 Jul 01 '16 at 20:19
  • There is a way to use something like the `where` clause, so that i can ask not for that specific protocol but any type that adopt that procol (Like `MySpecializedClassProtocol`) – Luca Bartoletti Jul 01 '16 at 20:21
  • Possible duplicate of [How to make protocol associated type require protocol inheritance and not protocol adoption](http://stackoverflow.com/questions/36867827/how-to-make-protocol-associated-type-require-protocol-inheritance-and-not-protoc) – Hamish Jul 01 '16 at 20:22
  • Your code is pretty old... e.g using `typealias` in the protocol instead of `associatedtype`. You should probably update your Swift version, especially given that it's a rapidly evolving language. – Hamish Jul 01 '16 at 20:23
  • Also see [this question](http://stackoverflow.com/questions/37360114/unable-to-use-protocol-as-associatedtype-in-another-protocol-in-swift), as well as the [filed bug report on this issue](https://bugs.swift.org/browse/SR-1581). – Hamish Jul 01 '16 at 20:29

0 Answers0