我有这两个函数:
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和do,但随后程序的其余部分中断了,因为变量没有初始化。
感谢您能提供的任何帮助。
发布于 2016-01-10 17:57:14
只需删除末尾的error
参数,并将其替换为try/catch
块。
do
{
moc.hasChanges
try moc.save()
}
catch let error as NSError
{
NSLog("Unresolved error \(error), \(error.userInfo)")
}
https://stackoverflow.com/questions/34704114
复制相似问题