Group Group Group Group Group Group Group Group Group

MulicastDelegate<T> improvements

#1

Hello,

Page 227:

  1. It looks like the func addDelegate(:) should cast delegate to AnyObject

    let wrapper = DelegateWrapper(delegate as AnyObject)
    delegateWrappers.append(wrapper)

  2. The func addDelegate(:) could have a check that would prevent adding the same object as a delegate twice. Currently, you can add the same object many times the the array.

Example:

    func addDelegate(_ delegate: ProtocolType) {
        if delegateWrappers.index(where: { $0.delegate === (delegate as AnyObject)
        }) != nil {
            assertionFailure("The object \(delegate) is already added as a delegate. Don't add it twice.")
            return
        }

        let wrapper = DelegateWrapper(delegate as AnyObject)
        delegateWrappers.append(wrapper)
    }
#2

@jrg.developer Can you please help with this when you get a chance? Thank you - much appreciated! :]

#3

Oh, good catch! I’ll get this fixed in the next version of the book.

True, you could do this! I’ll consider this for the next version too.

Thank you for your feedback. :]