doubi2228 2018-06-12 11:07
浏览 130

gomobile:绑定ObjC的回调

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?