Passing an Objective-C protocol to an objective-C function in Swift?

I'm trying to convert an old Obj-C project to Swift. I'm using an esoteric serialization protocol called Hessian that gets configured with calls like this:

[CWHessianUnarchiver setProtocol: @protocol(MissionProtocol) forClassName: @""];


@protocol MissionProtocol
// Properties…

In Swift, the calls look like this, but this isn’t quite right, and I'm not sure how to write it:

CWHessianUnarchiver.setProtocol(MissionProtocol.Type, forClassName: "")

But I get {Cannot convert value of type '(any MissionProtocol.Type).Type' to expected argument type 'Protocol?'" for the setProtocol call. How should I pass this?

Objective-C Protocol objects, like Swift metatypes, are spelled P.self, not P.Type.


The 2 posts may help solve your problem.

1 Like

I tried that, but it didn't work. However, I just tried it again, and it works. I must've not tried. But now I'm questioning my sanity. Thanks!