【问题标题】:Sharpie binding objective-c @protocols issueSharpie 绑定objective-c @protocols 问题
【发布时间】:2016-04-06 10:11:27
【问题描述】:

我正在使用sharpie bind 命令为xamariniOS 库获取API 接口

sharpie bind --namespace=XXX --sdk=iphoneos9.2 Headers/*.h

@protocol 绑定有问题:

The type or namespace name `IProfileDelegate' could not be found. Are you missing an assembly reference?

它是这样生成的:

    interface XLibrary : IProfileDelegate
    {
    [Wrap ("WeakProfileDelegate")]
    [NullAllowed]
    MB_ProfileDelegate ProfileDelegate { get; set; }

我知道它会创建空的 ProfileDelegate 然后编译器或其他东西用方法填充它,但我的问题是找不到 IProfileDelegate

@protocol ProfileDelegate <NSObject>
@required
- (void)GetProfileFinished:(NSString*)_data;
- (void)SetProfileFinished:(NSString*)_data;
@end

I 符号的区别(我猜这是为@protocols 保留的)。 如何让sharpie生成正确的api定义?

我能够删除所有 I 前缀并且它编译成功,但我宁愿修复它,不要在每次需要更新源库时重复此操作。

谢谢

【问题讨论】:

    标签: xamarin objective-sharpie


    【解决方案1】:

    请记住,所有 obj-c 协议都充当接口或抽象类,我建议将“协议、模型和设置基类型设置为 nsobject”,另一件事是将所有方法或属性设置为“必需”,您需要指定它是抽象的

    [Protocol, Model]
    [BaseType (typeof(NSObject))]
    interface myAwesomeDelegate
    {
      [Abstract]
      [Export(...)]
      void myRequiredMethod(uint param1)
    
      [Export(...)]
      void anotherMethod()
    }
    

    希望这能帮助您解决问题

    【讨论】:

    • 为什么要设置基类型?
    • @awattar 因为他的 ProfileDelegate obj-c 类有 NSObject 作为基类 (at)protocol ProfileDelegate
    • 但是什么时候协议不使用NSObject? stackoverflow.com/q/43126822/1142979
    • 设置 BaseType 为我修复了它。
    【解决方案2】:

    根据Objective Sharpie documentation

    在某些情况下,这些生成的文件可能就是您所需要的,但更多时候开发人员需要手动修改这些生成的文件以修复工具无法自动处理的任何问题(例如带有 @987654322 标记的问题@)。

    这意味着您有时必须调整生成的两个文件 ApiDefinitions.cs 和 StructsAndEnums.cs 来修复问题,例如本例中的问题。

    您可以阅读有关绑定如何在 Objective-C 协议中工作的更多信息,这些协议类似于 C# 接口,但不完全是 in the binding documentation

    【讨论】:

    • [verify ...] 指令很明确,但我的问题是关于这个前缀 I
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 2014-06-29
    相关资源
    最近更新 更多