program in playground can use find match () ?

,I want find the right program in playground can use find match () .

   @objc final private func findMatch() {

        let req = GKMatchRequest()

        req.minPlayers  = 2

        req.maxPlayers = 2

        if let vc = GKMatchmakerViewController(matchRequest: req) {

            vc.matchmakerDelegate = self

            present(vc, animated: true, completion: nil)

        } else {

            UIAlertController.showError(title: "Go To GameCenter", msg: "Matchmaking failed", vc: self)

        }

    } I can't use it in struct{} or playground ,it's say can only use in class ,how can I use it in struct ?

Replies

Just embed in a class:

class Test {
    @objc final func findMatch() {  // not private if you want to access it
         let req = GKMatchRequest()
         req.minPlayers  = 2
         req.maxPlayers = 2
         if let vc = GKMatchmakerViewController(matchRequest: req) {
             vc.matchmakerDelegate = self
             present(vc, animated: true, completion: nil)
         } else {
             UIAlertController.showError(title: "Go To GameCenter", msg: "Matchmaking failed", vc: self)
         }
     }
}

and use as:

let test = Test()
test.findMatch()

Note: where did you define showError ?