Search code examples
iosxcodeswiftdelegatesgame-center-leaderboard

Can't Implement GKGameCenterControllerDelegate


I have used GameViewController to authenticate the player, and upon coming into my Menu class and writing the protocol GKGameCenterControllerDelegate gives me an error, Type 'Menu' does not conform to protocol 'GKGameCenterControllerDelegate'. I don't understand what this error means and how to fix it. I have tried stuff from different places, but I don't think I understand how to do it right.

This is the line on which I'm getting error and is under the word Menu.

class Menu: SKScene, GKGameCenterControllerDelegate

Solution

  • The GKGameCenterControllerDelegate protocol, as the documentation specifies, has one method that is required: gameCenterViewControllerDidFinish. You need to implement this method in your Menu class.

    EDIT: In response to your comment, I'll provide a more in depth explanation.

    A protocol is an interface that includes a set of functions that are expected to be implemented. Anything that implements that protocol, called a delegate, is expected to define all of the functions in that set. The functions will be used by other classes that have a reference to your implementation and will call them as necessary.

    Think of it like working at a bank. You are a teller, and you are responsible for working with customers and handing out money. However, occasionally your drawer will run out of money to give, so you have to request your manager to retrieve more from the vault. Now, it doesn't matter which manager retrieves the cash for you. It can be your immediate one, his boss, all the way to the owner of the company. All you care about is being able to get cash so you can continue helping customers. You, as the teller, would have a protocol defined that includes a method like this: func requestCash(amount: Int) -> Int?. You would also have a delegate who is responsible for implementing that method or in our context, someone whose job it is to get you your cash. Now, whenever you run low on money, you have a way to get more.

    So, when you clock in to start your work as a teller, you require that your manager has the ability to get you cash. Let's say they hire a new guy who doesn't have the seniority to open the vault. Well, he's not going to work for you. When he says he will be your manager for the day (or implements your protocol), you know he can't actually get you cash from the vault (does not implement your method). So, you complain. Thus the error you saw.

    Note: you can have optional protocol methods. That's entirely valid (in our example above, it might be something like func approveLoanRequest(loan: Loan) -> Bool - you could simply ask customers to come back a different day to have a loan request approved, it's not necessary in your day-to-day). In that case, the methods are not called if they are not implemented and the class with the protocol do not throw errors if they are not implemented.