ObjC: binding callback

216 views
Skip to first unread message

fil.the....@gmail.com

unread,
Jun 12, 2018, 4:08:21 PM6/12/18
to golang-nuts

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

Elias Naur

unread,
Jun 13, 2018, 8:18:12 AM6/13/18
to golang-nuts
How are you subclassing the Go interface from ObjC? There is an example in the gomobile test, here:


The ObjC class "Number" implements the Go interface testpkg.I2 (the comment is wrong, I'll submit a CL to fix that)

 - elias

Philipp Maluta

unread,
Jun 13, 2018, 9:00:44 AM6/13/18
to golang-nuts, Elias Naur
Sure!
It’s the first way I tried. But it crashes. 
Because of this issue
13 июня 2018 г., 15:18 +0300, Elias Naur <elias...@gmail.com>, писал:
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/znKQiTpzPDU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Elias Naur

unread,
Jun 13, 2018, 9:06:06 AM6/13/18
to Philipp Maluta, golang-nuts
Issue 20254 is closed, so I assume this is a new problem. Please open a new issue with a small, self-contained demonstration of the crash. If it's easier for you, add to SeqTest.m and friends and include the diff in the report. Feel free to CC me (@eliasnaur) on the issue.

 - elias

Philipp Maluta

unread,
Jun 14, 2018, 3:35:32 AM6/14/18
to Elias Naur, golang-nuts
I’m sorry. I managed to figure out the issue.
Yes, it should be done on ObjC. But I did that on Swift.
I played with ObjC later and in other ways(Category, swizzling)
Thank you !
13 июня 2018 г., 16:05 +0300, Elias Naur <elias...@gmail.com>, писал:
Reply all
Reply to author
Forward
0 new messages