点击重启游戏后应用程序崩溃

时间:2015-07-07 02:19:41

标签: ios swift crash

当任一玩家按下“重启”按钮时,应用程序崩溃。我尝试添加一个警报,一旦Player2接受游戏重置,就会显示一条警告,并在OKAction行发送给Player1的新警报消息。

func handleReceivedDataWithNotification(notification:NSNotification){
    let userInfo = notification.userInfo! as Dictionary
    let receivedData:NSData = userInfo["data"] as! NSData

    let message = NSJSONSerialization.JSONObjectWithData(receivedData, options: NSJSONReadingOptions.AllowFragments, error: nil) as! NSDictionary
    let senderPeerId:MCPeerID = userInfo["peerID"] as! MCPeerID
    let senderDisplayName = senderPeerId.displayName

    if message.objectForKey("string")?.isEqualToString("New Game") == true{
        let alert = UIAlertController(title: "Game Restart Request", message: "\(senderDisplayName) has requested to restart the game", preferredStyle: UIAlertControllerStyle.Alert)
        let cancelAction = UIAlertAction(title: "No", style: .Cancel) { (acttion) in

        }

        alert.addAction(cancelAction)

        let OKAction = UIAlertAction(title: "Yes", style: .Default) { (action) in
            self.resetField()
             let messageDicts = ["string":"RestartNow"]
        }

        alert.addAction(OKAction)

        self.presentViewController(alert, animated: true) {
        }


    }else{
        var field:Int? = message.objectForKey("field")?.integerValue
        var player:String? = message.objectForKey("player") as? String

        if field != nil && player != nil{
            fields[field!].playerr = player
            fields[field!].setPlayer(player!)

            if player == "x"{
                currentPlayer = "o"
            }else{
                currentPlayer = "x"
            }

            checkResults()

        }

    }
}

这是Player2按下Accept / Yes时发送警报的地方:

@IBAction func newGame(sender: AnyObject, notification:NSNotification) {

    let alertWait = UIAlertController(title: "Request Sent", message: "Waiting for other player to accept this request", preferredStyle: UIAlertControllerStyle.Alert)

    self.presentViewController(alertWait, animated: true) {

    }


    let messageDict = ["string":"New Game"]

    let messageData = NSJSONSerialization.dataWithJSONObject(messageDict, options: NSJSONWritingOptions.PrettyPrinted, error: nil)

    var error:NSError?

    let userInfo = notification.userInfo! as Dictionary
    let receivedData:NSData = userInfo["data"] as! NSData

    let message = NSJSONSerialization.JSONObjectWithData(receivedData, options: NSJSONReadingOptions.AllowFragments, error: nil) as! NSDictionary
    let senderPeerId:MCPeerID = userInfo["peerID"] as! MCPeerID
    let senderDisplayName = senderPeerId.displayName

    if message.objectForKey("string")?.isEqualToString("RestartNow") == true{
        let alertOK = UIAlertController(title: "Game Is Restarting", message: "\(senderDisplayName) has accapted game request", preferredStyle: UIAlertControllerStyle.Alert)
        let OkayAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
            self.resetField()
            })

    self.presentViewController(alertOK, animated: true, completion: nil)
    }

    appDelegate.mpcHandler.session.sendData(messageData, toPeers: appDelegate.mpcHandler.session.connectedPeers, withMode: MCSessionSendDataMode.Reliable, error: &error)

    if error != nil{
        println("error: \(error?.localizedDescription)")
    } 
}

0 个答案:

没有答案