调用错误中的Swift2额外参数

调用错误中的Swift2额外参数,swift,swift2,Swift,Swift2,我有一个问题,这个代码我得到这个错误 “调用中的额外参数“错误” 我在发现错误的那一行做了标记 @IBAction func sendChat(sender: UIButton) { // Bundle up the text in the message field, and send it off to all // connected peers let msg = self.messageField.text!.dataUsingEncoding(NSUTF8StringEnc

我有一个问题,这个代码我得到这个错误

“调用中的额外参数“错误”

我在发现错误的那一行做了标记

@IBAction func sendChat(sender: UIButton) {
// Bundle up the text in the message field, and send it off to all
// connected peers

    let msg = self.messageField.text!.dataUsingEncoding(NSUTF8StringEncoding,
                                                       allowLossyConversion: false)

    var error : NSError?



    self.session.sendData(msg!, toPeers: self.session.connectedPeers,
        withMode: MCSessionSendDataMode.Unreliable, error: &error)


    if error != nil {
        print("Error sending data: \(error?.localizedDescription)")
    }

    self.updateChat(self.messageField.text!, fromPeer: self.peerID)

    self.messageField.text = ""
}

在Swift 2之前,我们通常使用如下语法:

var error: NSError?
session.sendData(msg!, toPeers: session.connectedPeers, withMode:.Unreliable, error: &error)
if error != nil {
    print("Error sending data: \(error?.localizedDescription)")
}
Swift 2错误处理范例采用
do
-
try
-
catch
(注意,没有
error
参数,因为错误现在被“抛出”,并在
catch
块中处理):

您在新版本的Xcode中使用旧语法,因此编译器警告您不再需要此
错误
参数

请参阅Swift编程语言的章节。

相关栏中充满了有关该错误的问题和答案。API不同,但解决方案总是相同的。
do {
    try session.sendData(msg!, toPeers: session.connectedPeers, withMode: .Unreliable)
} catch let error as NSError {
    print("Error sending data: \(error.localizedDescription)")
}