Infer generic from any using protocol with associatedtype

Pwyll28

i'm trying to make this code work

{ (tableview, originalItems, item, indexPath) in

    guard let matchingItem = originalItems.filter({ matching($0, with: item.itemIdentifier) }).first else {

        LogManager.Fatal.log("No item matching identifier : \(item.itemIdentifier)")

        return nil

    }
Some code
}

originalItems is [Any] and my function is

static func matching<T: SectionRowRepresentable>(_ item: T, with identifier: String) -> Bool where T.Identity == AnyItemRepresentable.Identity

How can I infer T from Any knowing the fact that SectionRowRepresentable has an associatedType Identity

public protocol SectionRowRepresentable: Equatable {

  associatedtype Identity: Hashable

  var itemIdentifier: String { get }

}
Pwyll28

I've ended up doing like this

  static func filter<T: SectionRowRepresentable>(_ items: [Any], match identifier: String) -> T? where T.Identity == AnyItemRepresentable.Identity {

    return items
      .flatMap { $0 as? T }
      .filter { $0.identity == identifier }
      .first

  }

  static func tableviewCellFactory() -> TableViewCellFactoryBlock {

    return { (tableview, originalItems, item, indexPath) in

      if let movieItem = filter(originalItems, match: item.itemIdentifier) as MovieItem? {

        let adapter = TitleLabelViewAdapter(mapping: movieItem.identifier, title: movieItem.title)
        let factory = TableViewCellFactory<TitleLabelView>(identifier: movieItem.identifier,
                                                           reuseIdentifier: TitleLabelView.ReuseIdentifier,
                                                           adapter: adapter)

        return AnyTableViewCellFactory(factory)

      } else if let adItem = filter(originalItems, match: item.itemIdentifier) as NativeAdItem? {

        let reuseIdentifier = "\(TopImageBottomTitleLabelView.ReuseIdentifier) \(adItem.identifier)"
        let adapter = TopImageBottomTitleLabelViewAdapter(mapping: adItem.identifier, title: adItem.title)
        let factory = TableViewCellFactory<TopImageBottomTitleLabelView>(identifier: adItem.identifier,
                                                                         reuseIdentifier: reuseIdentifier,
                                                                         adapter: adapter)

        return AnyTableViewCellFactory(factory)

      }

      LogManager.Fatal.log("No item matching identifier : \(item.itemIdentifier)")

      return nil

    }

  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using associatedtype in a delegate protocol for a generic type

How to write generic function in Swift using a protocol associatedtype

How to implement to a generic protocol, which has a function using the Type of associatedtype?

Protocol associatedType and <>

How to make extension to protocol using associatedtype and array

Is there information somewhere on Swift protocol associatedtype using `=` versus `:`?

How to check the concrete type of associatedtype of a protocol inside generic class?

Cannot assign Generic value to protocol associatedType variable in an extension

Infer any (generic) TypeScript class type from argument passed to a function

Infer generic from properties

Compiler Error Passing `associatedtype` value from one Protocol to another

Use Protocol with associatedtype as a Type

AssociatedType in a protocol with typealias

associatedtype protocol conformance issues

Swift protocol with associatedtype error

Attempting to use Protocol with associatedType

Infer generic type from argument

Protocol can only be used as a generic constraint because it has Self or associatedType requirements

Generic constraint for any protocol in Swift

Using delegates on generic protocol

Swift using generic in protocol

Swift protocol with associatedtype as a parameter type

Extension of a protocol where the associatedType is class?

Could not infer generic parameter when calling protocol static method

Is it possible to declare a protocol with an associatedtype inside a protocol?

Unable to use protocol as associatedtype in another protocol in Swift

Downcast from Any to a protocol

Infer generic parameter from base class

How to infer class from generic type in Java?