额外参数 Swift?
Extra argument Swift?
调用中的额外参数'error'
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) // This the error line
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!].Player(player!)
if player == "x"{
currentPlayer = "o"
}else{
currentPlayer = "x"
}
checkResults()
}
}
}
您可能正在尝试将 Swift 1.x 调用与 Swift 2.x 编译器一起使用。
Swift 在 1 和 2 之间经历了很大的变化,现在该方法(以及大多数其他报告错误的方法)抛出异常而不是传递错误参数。
current documentation 显示签名为:
class func JSONObjectWithData(_ data: NSData,
options opt: NSJSONReadingOptions) throws -> AnyObject
要了解如何处理异常,请查看 documentation。
调用中的额外参数'error'
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) // This the error line
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!].Player(player!)
if player == "x"{
currentPlayer = "o"
}else{
currentPlayer = "x"
}
checkResults()
}
}
}
您可能正在尝试将 Swift 1.x 调用与 Swift 2.x 编译器一起使用。
Swift 在 1 和 2 之间经历了很大的变化,现在该方法(以及大多数其他报告错误的方法)抛出异常而不是传递错误参数。
current documentation 显示签名为:
class func JSONObjectWithData(_ data: NSData,
options opt: NSJSONReadingOptions) throws -> AnyObject
要了解如何处理异常,请查看 documentation。