2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift2.0でエラー Cannot invoke 'sendData' with an argument list of type...の対処法

Posted at

エラーメッセージ

  • Swift1.2 → 2.0にしたところ、以下の文法エラーが出るようになりました
Cannot invoke 'sendData' with an argument list of type '(NSData?, toPeers: [MCPeerID], withMode: MCSessionSendDataMode, error: inout NSError?)'

原因

MCSession.h(swift1.2)
func sendData(_ data: NSData!, toPeers peerIDs: [AnyObject]!, withMode mode: MCSessionSendDataMode, error error: NSErrorPointer) -> Bool
MCSession.h(swift2.0)
func sendData(_ data: NSData, toPeers peerIDs: [MCPeerID], withMode mode: MCSessionSendDataMode) throws

対処法

  • APIを引数3つへ変更します。throwsに対応して、以下のように do~try~catch を実装します

Swift1.2

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

Swift2.0

swift2.0.swift
        do {
            try self.session.sendData(msg!,
            toPeers: self.session.connectedPeers,
            withMode: MCSessionSendDataMode.Unreliable)
        } catch {
            // do something.
        }
  • Objective-CのAPI変更では、非推奨期間があったりメソッドの宣言に飛べば「iOS7から変更になったよ」等コメントされていたと思うのですが、Swiftは変更が多いためそういったフォローがなくエラーメッセージが少し分かりにくいですね

参考

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?