swift - 如何修复 "does not conform to protocol"

标签 swift delegates xlpagertabstrip

我正在尝试实现 git 项目 XLPagerTabStrip .

根据项目,每个 Controller 必须:

Every view controller provided by PagerTabStripDataSource's viewControllers(for:) method must conform to InfoProvider

但是下面的代码抛出:不符合协议(protocol)

extension UserProfileSubController: IndicatorInfoProvider {

    func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {

        return IndicatorInfo(title: "UserProfileSubController")
    }
}

enter image description here

enter image description here

enter image description here

如果我想自动修复问题,它会重新实现相同的协议(protocol)功能,然后抛出无效的重新声明。

enter image description here

如果您的 Controller 确实符合协议(protocol),您如何解决不符合协议(protocol)的问题?我错过了什么?非常感谢帮助。

PS:我已经清理了项目、构建文件夹、删除了派生数据、重新启动并执行了 pod 更新以及 pod 的重新安装。

最佳答案

检查 IndicatorInfo 类如下所示:

public struct IndicatorInfo {

    public var title: String?
    public var image: UIImage?
    public var highlightedImage: UIImage?

    public init(title: String?) {
        self.title = title
    }

    public init(image: UIImage?, highlightedImage: UIImage? = nil) {
        self.image = image
        self.highlightedImage = highlightedImage
    }

    public init(title: String?, image: UIImage?, highlightedImage: UIImage? = nil) {
        self.title = title
        self.image = image
        self.highlightedImage = highlightedImage
    }

}

您使用公共(public)protocol IndicatorInfo 而不是公共(public)struct IndicatorInfo{}{}

而且我希望你只能在一个类中使用一个协议(protocol)。

extension YourViewController : IndicatorInfoProvider {

    // MARK: - Top Tab Bar Method - IndicatorInfoProvider
    func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
        return IndicatorInfo(title: "titleStringHere", image: UIImage(named: "Your_Image_Name"))
       /*or   return IndicatorInfo(title: "titleStringHere") */
    }
}

关于swift - 如何修复 "does not conform to protocol",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47184517/

相关文章:

c# - 异步触发的事件可以在表单上同步运行吗?

objective-c - 在 myproject-swift.h 中找不到接口(interface)声明

swift - XLPagerTabStrip:如何使用 ButtonBarPagerTabStripViewController 为不同的选项卡添加不同的栏按钮

swift - 如何禁用向上滑动和向下滑动

ios - UICollectionViewCell 中的 UICollectionView (Swift)

ios - CLLocationManager 的委托(delegate)函数 'didRangeBeacon' 没有找到任何信标?

ios - 将 TableView 数据导出到 CSV 文件

ios - 如何使用 IQKeyboardManager 跳过隐藏的 UITextField?

ios - 如何为 UIPageViewController Delegate 创建自定义类?

c# - 从回调调用时,应如何为非托管代码返回委托(delegate)?