我正在尝试实现 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")
}
}
如果我想自动修复问题,它会重新实现相同的协议(protocol)功能,然后抛出无效的重新声明。
如果您的 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/