4

In Swift 4.0 I could write something like this

protocol ObserversHolder {

    ///Compiling Error in Swift 4.1
    ///note: possibly intended match 'StringManager.ObserverValue' (aka 'StringObserver') does not conform to 'AnyObject'
    ///note: protocol requires nested type 'ObserverValue'; do you want to add it?
    associatedtype ObserverValue: AnyObject

    var observers: [ObserverValue] {get set}
}

protocol StringObserver: class {
    func showString()
}

class StringManager: ObserversHolder {
    typealias ObserverValue = StringObserver

    var observers = [ObserverValue]()
}

But in Swift 4.1 I receive the error Type 'StringManager' does not conform to protocol 'ObserversHolder'.

Is it possible to resolve this?

Vladislav
  • 73
  • 6
  • 4
    This is a consequence of [Protocol doesn't conform to itself?](https://stackoverflow.com/q/33112559/1187415): `StringObserver` *inherits* from `AnyObject`, but does not *conform* to it. – Martin R May 25 '18 at 17:17

1 Answers1

0

Change AnyObject to Any

protocol ObserversHolder {

    ///Compiling Error in Swift 4.1
    ///note: possibly intended match 'StringManager.ObserverValue' (aka 'StringObserver') does not conform to 'AnyObject'
    ///note: protocol requires nested type 'ObserverValue'; do you want to add it?

    associatedtype ObserverValue: Any

    var observers: [ObserverValue] {get set}
}

protocol StringObserver: class {
    func showString()
}

class StringManager: ObserversHolder {
    typealias ObserverValue = StringObserver

    var observers = [ObserverValue]()
}
Nader
  • 1,120
  • 1
  • 9
  • 22