简体   繁体   中英

Can't create protocol that is restricted to Classes in Xcode 9.3 / Swift 4.1

I am trying to store a list of subscribers that are ThemeListeners (mostly UIViews or UIViewControllers) and I need these to be stored weakly, otherwise the UIViewControllers are never released and I get memory leaks. I took the WeakRef class from

https://marcosantadev.com/swift-arrays-holding-elements-weak-references/

When I put this into my project and try and compile it I get an error on the last line in Xcode 9.3:

'WeakRef' requires that 'ThemeListener' be a class type

Compiling this in Xcode 9.2 works.

class WeakRef<T> where T:AnyObject
{
    private(set) weak var value : T?

    init( value:T?)
    {
        self.value = value
    }
}

protocol ThemeListener : AnyObject
{
}

typealias WeakRefThemeListener = WeakRef<ThemeListener>

Does anyone have any suggestions on how to fix this. I also have the same problem when trying to use NSHashTable.

I think your protocol should be like this:

protocol ThemeListener : class
{}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM