Search code examples
swiftgenericsprotocolsstandards-compliance

A protocol is not conforming to a class inherited from a generic parent class?


I am trying to conform a class to a protocol however, I am getting error. Here is the code:

class UserSearchViewController: GenericSearchViewController<User> 

I am trying to conform it to a buttontappeddelegate:

extension UserSearchViewController: ButtonDidGetTappedDelegate {
func button(wasTappedInCell cell: UserCollectionViewCell) {
    print("Cell Tapped")
   }
}

I get the error when I assign self to the delegate:

adapter.delegate = self 

Cannot assign value of type 'UserSearchViewController.Type' to type 'ButtonDidGetTappedDelegate?'

What is the issue here? Any help would be appreciated.

class UserSearchViewController: GenericSearchViewController<User> {
    static func searchV (Config: ConfigurationProtocol,
                     dataSource: DataSource,
                     viewer: User) ->  UserSearchViewController
  let vc = UserSearchViewController(Config, dataSource,viewer) 
   ......
   ...
  adapter.delegate = self
  return vc

}

Solution

  • Because you are doing this in a static function, you can't use self to refer to an instance of your VC. Instead, you have an instance of your VC already! It's vc.

    Just set vc as the delegate instead of self:

    adapter.delegate = vc