-2

Extra argument 'error' in call

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()

            }

        }


    }
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Jeylani Osman
  • 199
  • 3
  • 13
  • To the people voting to close the question, no, it is clear what is being asked. – TofuBeer May 28 '16 at 14:07
  • Swift changed to using `throws` instead of in-out errors. See https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html – Phillip Mills May 28 '16 at 14:11
  • 1
    There are dozens of questions about "Extra argument 'error' in call" (e.g. in the Related list). A search for "JSONObjectWithData Extra argument 'error' in call" reveals the solution immediately. – Martin R May 28 '16 at 14:12

1 Answers1

1

You are probably trying to use a Swift 1.x call with a Swift 2.x compiler.

Swift went through a large change between 1 and 2, now the method (and most others that report errors) throws an exception instead of passing in an error argument.

The current documentation shows the signature as:

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

To see how to hand exceptions look at the documentation.

TofuBeer
  • 60,850
  • 18
  • 118
  • 163