quantumpotato
quantumpotato

Reputation: 9767

Gamecenter setting authenticate handler crashes

func authenticateLocalPlayer(player: GKLocalPlayer) {
        player.authenticateHandler =
            ({ (viewController : UIViewController!, error : NSError!) -> Void in
                if viewController != nil {
                    self.present(viewController, animated:true, completion: nil)
                } else {

                }
                } as! (UIViewController?, Error?) -> Void)

    }

this freezes on that last line -- as! (UIViewController?, Error?) -> Void)

with nothing in the stack trace beyond that.. just freezes execution. What's happening here?

How do I set the authenticate handler?

mouseover of the freeze: Thread 1: EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)

Upvotes: 3

Views: 77

Answers (1)

deekay
deekay

Reputation: 929

It is because you declared the wrong handler and attempted to fix it by force-casting it to as! (UIViewController?, Error?) -> Void which obviously will fail, as these types are not the same.

That's how it should look like:

player.authenticateHandler =
        ({ (viewController : UIViewController?, error : Error?) -> Void in
            if let vc = viewController {
                self.present(vc, animated:true, completion: nil)
            } else {

            }
        })

Make sure you do not trust Xcode too much and double check types yourself.

Upvotes: 3

Related Questions