我有一个Go界面
type GetResponse interface { OnResult(json string) }
我必须使用此界面订阅来自ObjC的事件OnResult。
func Subscribe( response GetResponse){ response.OnResult("some json") }
ObjC bind为我提供了相应的协议和基本类
@interface GetResponse : NSObject <goSeqRefInterface, GetResponse> {
}
@property(strong, readonly) id _ref;
- (instancetype)initWithRef:(id)ref;
- (void)onResult:(NSString*)json;
@end
所以,我需要在我的ObjC环境中获得这个json。我怎么能这样做?
类别如果我在Go方面使用协议支持创建结构,我不能将其子类化,但至少它不会崩溃:
type GetResponseStruct struct{}
func (GetResponseStruct) OnResult(json string){log.Info("GO RESULT")}
func CreateGetResponse() *GetResponseStruct{ return &GetResponseStruct{}}
我有一个坚实的对象,没有明显的方法来连接我的回调。如果我创建一个类别并覆盖onResult例程,则不会调用它。仅仅因为根据AppleDoc重写现有的类方法并不是确定的行为。无论何时OnResult从Go调用,默认实现都会调用&#34; GO RESULT&#34;出现。
Swizzling 我尝试使用类别和混合(使用我重命名的方法替换方法&#39;实现)但只有从ObjC env调用onResult时它才有效。
有什么方法可以解决我的问题吗?或者我只是不太准确地红色文档?请帮帮我
答案 0 :(得分:1)
我今天遇到了类似的问题。我以为这是gomobile中的错误,但这毕竟是我对swift / objc的误解。 https://github.com/golang/go/issues/35003
该错误的TLDR是您必须继承协议的子类,而不是接口的子类。如果使用的是swift,则可以将GetResponseProtocol
子类化:
class MyResponse: GetResponseProtocol
如果在objc中,则可能要直接实现GetResponse协议,而不是对接口进行子类化。