0

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

}
Osama Naeem
  • 1,830
  • 5
  • 16
  • 34
  • 1
    Where are you writing the line `adapter.delegate = self`? Can you show the surrounding code? The error message makes me suspect that you are writing that line in a static context. – Sweeper May 30 '19 at 20:29
  • @Sweeper The line is in a static function. That static function returns the view controller of type UserSearchViewController. I will update the code. – Osama Naeem May 30 '19 at 20:32
  • 1
    Also, you seem to be extending `ChatUserSearchViewController`, but you said you wanted to make `UserSearchViewController` (without `Chat`) conform to the protocol. – juliand665 May 30 '19 at 20:34
  • @Sweeper Just updated the code – Osama Naeem May 30 '19 at 20:36
  • @juliand665 Corrected it! Thats not an issue. – Osama Naeem May 30 '19 at 20:37
  • It was probably part of the issue. Combined with Sweeper's answer, that should do the trick. – juliand665 May 30 '19 at 20:39

1 Answers1

0

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
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • To clarify, being in a static context means that you're not within any specific instance of your class (in this case, you're only just creating such an instance). This means that `self` refers to the _type_ `UserSearchViewController` as a whole, rather than an _instance_ of it, but only instances of types can conform to protocols. – juliand665 May 30 '19 at 20:42
  • @juliand665 If I want to use the instance of the class elsewhere within a class, is that possible? For example I create a variable of type UserSearchViewController? within the class. Is it possible to assign vc to that variable? – Osama Naeem May 30 '19 at 20:51
  • 1
    Yes. That's exactly where you'd use `self`—in a non-static context (i.e. whenever the function doesn't have the word `static` on it), `self` refers to the instance you're currently "in", rather than the type itself. – juliand665 May 30 '19 at 20:52
  • 1
    If you're still in a static context, you can of course access it like `vc.myVariable = vc` – juliand665 May 30 '19 at 20:53