Swift 到 Swift 2 [重复]

Posted

技术标签:

【中文标题】Swift 到 Swift 2 [重复]【英文标题】:Swift to Swift 2 [duplicate] 【发布时间】: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 并执行,但随后程序的其余部分因变量未初始化而中断。

感谢您提供的任何帮助。

【问题讨论】:

【参考方案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 块而不是错误参数。我编辑了我的问题,以便您可以轻松查看预期内容。

以上是关于Swift 到 Swift 2 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何将字段添加到 NSMutableDictionary Swift [重复]

在 Swift 中重复 alpha 动画

Swift:重复的连续 CABasicAnimations

为啥我的 Haskell 代码与 Swift 和 C 相比如此缓慢 [重复]

在 Swift 2 中用啥代替 stringByAppendingPathComponent [重复]

Swift 2.0 - stringByAppendingPathComponent 错误转换 [重复]