我试图创建通用的UIPageViewControlller,为此我想在协议扩展中使用默认实现,但我得到了这个堰错误
Objective-C方法'pageViewController:viewControllerBefore:'提供 通过方法'pageViewController(_:viewControllerBefore :)'不匹配 需求的选择器 ( 'pageViewController:viewControllerBeforeViewController:')
这就是我的代码的样子
protocol ViewModel {}
protocol PagerCard: class {
var viewModel: ViewModel? { get set }
}
protocol Pager: UIPageViewControllerDataSource {
associatedtype Card: UIViewController, PagerCard
associatedtype CardModel: ViewModel
var pagerController: UIPageViewController! { get set }
var cards: Array<Card> { get set }
var cardModels: Array<CardModel> { get set }
func cardFactory() -> Card
func viewControllerAtIndex(_ index: Int) -> Card?
}
extension Pager {
func viewControllerAtIndex(_ index: Int) -> Card? {
if (cardModels.count == 0) ||
(index >= cardModels.count || index < 0) {
return nil
}
// Get already configured controller
if self.cards.count > index {
return self.cards[index]
}
let cardVC = self.cardFactory()
cardVC.viewModel = cardModels[index]
cards.append(cardVC)
return cardVC
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let index = self.cards.index(of: viewController as! Card )!
return self.viewControllerAtIndex(index - 1)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let index = self.cards.index(of: viewController as! Card)!
return self.viewControllerAtIndex(index + 1)
}
}
问题在于最后两种方法的Pager扩展,xcode抱怨函数声明但它们与UIPageViewControllerDataSource完全相同,如果我在UIViewController中使用完全相同的代码,一切正常。 有任何想法吗??谢谢