简体   繁体   中英

Argument type does not conform to expected type

I want a generic UIPageViewControllerDataSource that can do its thing based on two generic parameters, like so:

protocol ItemDisplaying where Self: UIViewController {
  associatedtype T

  var item: T { get }

  init(item: T)
}

final class LinearPageDataSource<V: ItemDisplaying> {
  let items: [V.T]

  init(items: [V.T]) {}
}

However, a method within the data source like this:

private func indexOf(_ viewController: V) -> Int {
  return items.firstIndex(of: viewController.item) ?? NSNotFound
}

Errors out with Argument of type VT does not conform to expected type 'Equatable' . Therefore, I added explicitly this code: class OnboardingContentViewController<T: Codable & Equatable>: UIViewController, ItemDisplaying so I can show that the type of the item passed in as T in the ViewController is Codable & Equatable .

I'm getting stuck in what I'm trying to achieve, I'd like to have a data source that takes an array of items that are at least Codable & Equatable and I'd like to keep a generic possibility for a type of UIViewController to be passed in. How can I best achieve this?

I didn't notice that an associatedtype can't refer to another protocol, but has to refer to a concrete type. Therefore the following works:

protocol ItemDisplaying where Self: UIViewController {
  associatedtype T: Decodable & Equatable

  var item: T { get }

  init(item: T)
}
final class LinearPageDataSource<V: ItemDisplaying>: NSObject, UIPageViewControllerDataSource {
  let items: [V.T]

  init(items: [V.T]) {
    self.items = items
  }

  func contentViewController(atIndex index: Int) -> V? {
    return V.init(item: items[index])
  }
final class OnboardingContentViewController: UIViewController, ItemDisplaying {
  typealias T = OnboardingItem // this is a struct, conforming to Decodable and Equatable

  let item: T

  required init(item: T) {
    self.item = item
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM