jonasman / MulticastDelegate

An elegant multicast delegate written in swift
MIT License
149 stars 28 forks source link

Cannot remove non-class delegates #12

Closed arthurhammer closed 8 years ago

arthurhammer commented 8 years ago

Example:

protocol ServiceDelegate { }

class Service {
    var delegate = MulticastDelegate<ServiceDelegate>(strongReferences: true)
}

struct Struct: ServiceDelegate { }

let service = Service()
let x = Struct()

service.delegate.addDelegate(x)
service.delegate.removeDelegate(x)
// Not empty and you can't get the thing out ever again

Basically I'm wondering, why the generic type T is not restricted to classes.

jonasman commented 8 years ago

The generic type accepts protocols. That is why there is no restriction. In swift we cannot restrict to a protocol only.

Later i will check the bug you mention!

arthurhammer commented 8 years ago

Thanks for your response. I found this comment in the tests. I guess using structs is indeed not indented (as I think it shouldn't be). It's unfortunate that we cannot restrict this in the API.

jonasman commented 8 years ago

Correct you are not supposed to add structs or enums as delegates.

arthurhammer commented 8 years ago

Thanks!