首页 > 解决方案 > 'ViewController' 不能转换为 'CocoaMQTTDelegate'?

问题描述

我正在尝试使用 CocoaMQTT 建立连接。GitHub 页面上提供了以下示例:

https://github.com/emqx/CocoaMQTT/blob/master/Example/Example/ViewController.swift

他们使用视图控制器“self”作为委托(参见第 53/63/74 行)。但是,当我尝试使用视图控制器作为委托时,我得到了错误:

Cannot assign value of type 'ViewController' to type 'CocoaMQTTDelegate?'

我尝试使用以下方法转换它:

self as! CocoaMQTTDelegate

但没有骰子。

在示例中,他们使用的ViewController: UIViewController是 iOS 版,而我使用的ViewController: NSViewController是 macOS 版,这会导致问题吗?我对 Swift 很陌生,所以任何帮助都将不胜感激。(它是一个 iOS/macOS/tvOS 库)。

有任何想法吗?

标签: swiftxcodemacoscocoamqtt

解决方案


感谢@Vollan 建议在文件底部添加扩展名。我可能应该注意到这一点。我的错。


推荐阅读