2

I have a Go interface

type GetResponse interface { OnResult(json string) }

I have to subscribe on that event OnResult from ObjC using this interface.

func Subscribe( response GetResponse){ response.OnResult("some json") }

ObjC bind gives me a corresponding protocol and a basic class

@interface GetResponse : NSObject <goSeqRefInterface, GetResponse> {
}
@property(strong, readonly) id _ref;

- (instancetype)initWithRef:(id)ref;
- (void)onResult:(NSString*)json;
@end

So, I need to get this json in my ObjC env. How can I do that?

  1. Subclassing If I subclass this GetResponse or just use it as is and pass to Subscribe routine, it crashes 'go_seq_go_to_refnum on objective-c objects is not permitted'
  2. Category if I create struct on Go side with the protocol support, I can't subclass it but at least it's not crashes:

    type GetResponseStruct struct{}
    func (GetResponseStruct) OnResult(json string){log.Info("GO RESULT")}
    func CreateGetResponse() *GetResponseStruct{ return &GetResponseStruct{}}
    

    I have a solid object without obvious way to hook up my callback. If I make a category and override the onResult routine, it's not called. Just because overriding existing methods of class is not determined behavior according to AppleDoc. Anytime OnResult called from Go, the default implementation invokes and "GO RESULT" appears.

  3. Swizzling I tried to use category and swizzle (replace method's implementation with mine renamed method) but it only works if I call onResult from ObjC env.

Any way to solve my issue? Or I just red the doc not very accurately? Please help me

ensoreus
  • 21
  • 2

1 Answers1

2

I ran into a similar issue today. I thought it was a bug in gomobile but it was my misunderstanding of swift/objc after all. https://github.com/golang/go/issues/35003

The TLDR of that bug is that you must subclass the protocol, and not the interface. If you are using swift then you can subclass the GetResponseProtocol:

class MyResponse: GetResponseProtocol

If in objc, then you probably want to directly implement the GetResponse protocol, rather than subclassing the interface.

jonr
  • 1,133
  • 1
  • 11
  • 25
  • Any chance you could explain this a bit more in detail? I'm running into the same issue and I'm trying to extend the my own interface's Protocol but I don't know enough Swift to understand what I'm supposed to do – Guru Prasad Jun 07 '23 at 21:20