Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define method with closure in an @objc protocol

I have the following protocol definition:

@objc protocol PersonDataStore {

    func findPersonWithId(remoteId: String, completionBlock: ((Person) -> Void)!)
    // ...
}

The error I get is that the second parameter cannot be represented in Objective-C. I researched the blocks/closures topic but I have a hard time getting my head around it since it is so conceptually different.

Is this just a matter of syntax or is it really not possible to define an Objective-C protocol with a closure in Swift?

like image 857
Sebastian Wramba Avatar asked May 02 '26 00:05

Sebastian Wramba


1 Answers

I believe your problem is that your class Person is not a subclass of NSObject. Add @objc to the declaration of your Person class or make it a subclass of NSObject and your protocol definition should work.

like image 144
vacawama Avatar answered May 03 '26 18:05

vacawama