Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift optional method in protocol without objc

Tags:

swift

I know, that making some methods in a Swift protocol requires to use @objc protocol. The problem is, that I can't use the objective c way, because I have a method in the protocol, which returns a Swift struct. So I'm getting the error, that I can't use the @objc protocol, because my method returns a result, which cannot be represented in objective c.

Unfortunately, I absolutely want to use an optional methods, because there are two methods, which are alternative and the user of my class shall choose, which way he want to use.

like image 914
Lupurus Avatar asked Jul 13 '26 07:07

Lupurus


1 Answers

What I do in this situation is return a class (compatible with Objective-C) which wraps the Swift struct. In other words, just make it so that everything Objective-C sees is compatible with Objective-C, and inside that you can do anything you like.

like image 109
matt Avatar answered Jul 14 '26 22:07

matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!