额外参数Swift?

时间:2016-05-28 14:00:17

标签: ios xcode swift swift2.2

额外的争论'错误'在电话中

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) // This the error line
        let senderPeerId:MCPeerID = userInfo["peerID"] as! MCPeerID
        let senderDisplayName = senderPeerId.displayName

        if message.objectForKey("string")?.isEqualToString("New Game") == true{
            let alert = UIAlertController(title: "TicTacToe", message: "\(senderDisplayName) has started a new Game", preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

            self.presentViewController(alert, animated: true, completion: nil)

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

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

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

                checkResults()

            }

        }


    }

1 个答案:

答案 0 :(得分:1)

您可能正在尝试使用Swift 1.x编译器进行Swift 1.x调用。

Swift经历了1到2之间的大变化,现在该方法(以及报告错误的大多数其他方法)抛出异常而不是传入错误参数。

current documentation将签名显示为:

 class func JSONObjectWithData(_ data: NSData,
                  options opt: NSJSONReadingOptions) throws -> AnyObject 

要了解如何处理异常,请查看documentation