bugfix> swift > 投稿

Swift 4.0では、このようなものを書くことができました

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]()
}

しかし、Swift 4.1ではエラー Type 'StringManager' does not conform to protocol 'ObserversHolder' が表示されます 。

これを解決することは可能ですか?

回答 1 件
  • AnyObject を変更   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]()
    }
    
    

あなたの答え