首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swift到Swift 2

Swift到Swift 2
EN

Stack Overflow用户
提问于 2016-01-10 17:55:45
回答 1查看 173关注 0票数 1

我有这两个函数:

代码语言:javascript
运行
复制
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中运行它们,因为我现在已经升级了,我得到了以下错误:

代码语言:javascript
运行
复制
Extra argument 'error' in call.

我在以下几行得到了这个错误:

代码语言:javascript
运行
复制
let message = NSJSONSerialization.JSONObjectWithData(receivedData, options: NSJSONReadingOptions.AllowFragments, error: nil) as NSDictionary

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

我需要做些什么来修复这些问题?

我试着添加了try、catch和do,但随后程序的其余部分中断了,因为变量没有初始化。

感谢您能提供的任何帮助。

EN

回答 1

Stack Overflow用户

发布于 2016-01-10 17:57:14

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

代码语言:javascript
运行
复制
do
{
  moc.hasChanges
  try moc.save()
}
catch let error as NSError
{
  NSLog("Unresolved error \(error), \(error.userInfo)")
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34704114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档