1
votes

This works:

protocol Inconceivable {
    associatedtype AbstractType
    func say(it: AbstractType)
}

class Vizzini: Inconceivable {
    func say(theWord: String) {
        print(theWord)
    }
}

Vizzini().say("Inconceivable!")

However, modifying the protocol to use optional prefix:

@objc protocol Inconceivable {
    associatedtype AbstractType
    optional func say(it: AbstractType)
}

It doesn't work anymore:

error: method cannot be a member of an @objc protocol because the type of the parameter cannot be represented in Objective-C optional func say(it: AbstractType)

Is there a workaround?

1

1 Answers

3
votes

The optional part itself isn't the issue, it's the @objc which is the problem. The associated type is a generic component of the protocol and objective-c doesn't have generics so you can't export a protocol using them. The only way you can work around it is to change the protocol.