Ios Swift对Swift 2
我有两个功能:Ios Swift对Swift 2,ios,swift,Ios,Swift,我有两个功能: func handleReceivedDataWithNotification(notification:NSNotification){ let userInfo = notification.userInfo! as Dictionary let receivedData:NSData = userInfo["data"] as! NSData let message = NSJSONSerialization.JSONObj
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-and-catch-and-do,但是由于变量没有初始化,程序的其余部分中断了
感谢您提供的帮助。只需删除结尾处的
error
参数,并将其替换为try/catch
块即可
do
{
moc.hasChanges
try moc.save()
}
catch let error as NSError
{
NSLog("Unresolved error \(error), \(error.userInfo)")
}
只需删除末尾的
error
参数,并将其替换为try/catch
块
do
{
moc.hasChanges
try moc.save()
}
catch let error as NSError
{
NSLog("Unresolved error \(error), \(error.userInfo)")
}
调用可以抛出,但没有标记try
是的,有些调用现在使用try-catch
块而不是errors args。我编辑了我的问题,这样你就可以很容易地看到预期的结果。调用可以抛出,但没有标记try
是的,现在有些调用使用try-catch
块而不是错误参数。我编辑了我的问题,这样你就可以很容易地看到预期的结果。