【问题标题】:Swift ViewController, objective C protocol, Type 'ViewController' does not conform to protocol 'MyProtocolDelegate'Swift ViewController,目标 C 协议,类型“ViewController”不符合协议“MyProtocolDelegate”
【发布时间】:2016-11-17 16:18:41
【问题描述】:

我正在开发具有 swift ViewController、使用 C++ 代码和目标 C 类作为 C++ 类的包装器的混合项目。到目前为止开发的部分工作。 我在 Objective C 中添加了简单的协议,它只声明了一种方法:

@protocol MyProtocolDelegate <NSObject>
  - (void)updateCount:(int)count;
@end
@interface CvVideoCameraWrapper : NSObject
@property (weak,nonatomic) id <MyProtocolDelegate> delegate; 
    //remaining of my interface
@end

在 swift 代码中,我将 MyProtocolDelegate 添加到 ViewController 遵守的其他协议并添加:

class ViewController: UIViewController, CvVideoCameraDelegate, UITextFieldDelegate, MyProtocolDelegate{

    func updateCount(count:Int)
    {
        return
    }
    override func viewDidLoad() {
        super.viewDidLoad()        
        self.typedName.delegate = self // this one works
        self.videoCameraWrapper.delegate = self 
        // remaining of my viewDidLoad
    }
        //remaining of my ViewController
}

编译器显示错误: 类型“ViewController”不符合协议“MyProtocolDelegate”

我不知道目标 C 中声明的委托方法签名和 swift 匹配,如果目标 C 中的 int 类型和 swift 中的 Int 相同,但我知道 swift 会桥接和我的其他 swift 交互,客观的 C 和 C++ 作品 请指出此协议使用有什么问题

【问题讨论】:

  • ObjC int 相当于 Swift Int32 我相信。
  • 确保你已经在MyProtocolDelegate中实现了所有需要的方法
  • 你应该在你的协议方法声明中使用NSInteger
  • 谢谢!我已经以另一种方式改变了它。使 swift updateCount 接受 Int32 作为参数。它有效。

标签: ios objective-c swift uiviewcontroller delegates


【解决方案1】:

swift 类型 Int32 是 obj-c int 的正确等价物

但是 imo 你应该使用 C 类型的别名,以避免混淆。 在你的例子中它会是

func updateCount(count:CInt)
{
    return
}

看看Apple - Interacting with C APIs

【讨论】:

    猜你喜欢
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多