【问题标题】:Swift to Swift 2 [duplicate]Swift 到 Swift 2 [重复]
【发布时间】:2016-01-10 09:55:45
【问题描述】:

我有这两个功能:

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: "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!].setPlayer_1(player!)

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

                checkResults()

            }

        }


    }


    func fieldTapped (recognizer:UITapGestureRecognizer){
        let tappedField  = recognizer.view as! TTTImageView
        tappedField.setPlayer_1(currentPlayer)

        let messageDict = ["field":tappedField.tag, "player":currentPlayer]

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

        var error:NSError?

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

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

        checkResults()


    }

如果我因为现在已经升级而尝试在 Swift 2 中运行它们,我会收到以下错误:

Extra argument 'error' in call.

我在以下几行中收到该错误:

let message = NSJSONSerialization.JSONObjectWithData(receivedData, options: NSJSONReadingOptions.AllowFragments, error: nil) as NSDictionary

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

我需要做什么来解决这些问题?

我尝试添加一个 try 和 catch 并执行,但随后程序的其余部分因变量未初始化而中断。

感谢您提供的任何帮助。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    只需删除末尾的error 参数,并将其替换为try/catch 块。

    do
    {
      moc.hasChanges
      try moc.save()
    }
    catch let error as NSError
    {
      NSLog("Unresolved error \(error), \(error.userInfo)")
    }
    

    【讨论】:

    • call can throw but is not marked with try
    • 是的,一些调用现在使用try - catch 块而不是错误参数。我编辑了我的问题,以便您可以轻松查看预期内容。
    猜你喜欢
    • 2016-06-03
    • 1970-01-01
    • 2017-02-06
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    相关资源
    最近更新 更多