我正在尝试this tutorial,由于新的swift版本出现了一些错误,其中一个是MPCManager不符合协议MCNearbyServiceAdvertiserDelegate。
我尝试修复它,所以我将func广告客户的标题更改为此标题:
func广告客户(广告客户:MCNearbyServiceAdvertiser, didReceiveInvitationFromPerer peerID:MCPeerID,withContext context: NSData?,invitationHandler :( Bool,MCSession) - > Void){
然后它却引发了我的错误
无法赋值'(Bool,MCSession) - >无效'打字'(布尔, MCSession!) - >无效!
我不知道如何解决,帮助赞赏!
实际档案:
import UIKit
import MultipeerConnectivity
protocol MPCManagerDelegate {
func foundPeer()
func lostPeer()
func invitationWasReceived(fromPeer: String)
func connectedWithPeer(peerID: MCPeerID)
}
class MPCManager: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate {
var session: MCSession!
var peer: MCPeerID!
var browser: MCNearbyServiceBrowser!
var advertiser: MCNearbyServiceAdvertiser!
var foundPeers = [MCPeerID]()
var invitationHandler: (Bool, MCSession!)->Void!
var delegate: MPCManagerDelegate?
override init() {
super.init()
invitationHandler(false, nil)
peer = MCPeerID(displayName: UIDevice.currentDevice().name)
session = MCSession(peer: peer)
session.delegate = self
browser = MCNearbyServiceBrowser(peer: peer, serviceType: "appcoda-mpc")
browser.delegate = self
advertiser = MCNearbyServiceAdvertiser(peer: peer, discoveryInfo: nil, serviceType: "appcoda-mpc")
advertiser.delegate = self
}
func browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
foundPeers.append(peerID)
delegate?.foundPeer()
}
func browser(browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
for (index, aPeer) in EnumerateSequence(foundPeers){
if aPeer == peerID {
foundPeers.removeAtIndex(index)
break
}
}
delegate?.lostPeer()
}
func browser(browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: NSError) {
print(error.localizedDescription)
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ THE Error occurs here: ~~~~~~~~~~~~~~~~~~~~~~
func advertiser(advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: NSData?, invitationHandler: (Bool, MCSession)->Void) {
self.invitationHandler = invitationHandler
delegate?.invitationWasReceived(peerID.displayName)
}
func advertiser(advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: NSError) {
print(error.localizedDescription)
}
func session(session: MCSession, peer peerID: MCPeerID, didChangeState state: MCSessionState) {
switch state{
case MCSessionState.Connected:
print("Connected to session: \(session)")
delegate?.connectedWithPeer(peerID)
case MCSessionState.Connecting:
print("Connecting to session: \(session)")
default:
print("Did not connect to session: \(session)")
}
}
func sendData(dictionaryWithData dictionary: Dictionary<String, String>, toPeer targetPeer: MCPeerID) -> Bool {
let dataToSend = NSKeyedArchiver.archivedDataWithRootObject(dictionary)
let peersArray = NSArray(object: targetPeer) as! [MCPeerID]
do {
_ = try session.sendData(dataToSend, toPeers: peersArray, withMode: MCSessionSendDataMode.Reliable)
} catch let error as NSError {
print(error.localizedDescription)
return false
}
return true
}
func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID) {
let dictionary: [String: AnyObject] = ["data": data, "fromPeer": peerID]
NSNotificationCenter.defaultCenter().postNotificationName("receivedMPCDataNotification", object: dictionary)
}
func session(session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, withProgress progress: NSProgress) { }
func session(session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, atURL localURL: NSURL, withError error: NSError?) { }
func session(session: MCSession, didReceiveStream stream: NSInputStream, withName streamName: String, fromPeer peerID: MCPeerID) { }
}
答案 0 :(得分:0)
您使用invitationHandler
类型定义了(Bool, MCSession!)->Void!
变量,然后为其分配值(Bool, MCSession)->Void)
类型。这是导致您的错误消息的原因。
要解决此问题,请使用带有advertiser
变量的参数匹配类型定义invitationHandler
方法。
答案 1 :(得分:0)
类invitationHandler
的属性MPCManager
定义为:
var invitationHandler: (Bool, MCSession!) -> Void!
... invitationHandler
方法的参数advertiser
定义为
invitationHandler: (Bool, MCSession) -> Void
这里有两个不匹配:
Void!
,而参数返回Void
MCSession!
,而方法需要MCSession
首先,我认为将Void!
作为返回值并不重要。 Void
表示根本没有返回任何内容,因此无需隐式拆开它。
其次,参数的类型必须匹配。因此,两者都应为MCSession
或MCSession!
。
E.g。以下内容不会产生您提到的错误:
var invitationHandler: (Bool, MCSession!) -> Void
// ...
func advertiser(advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: NSData?,
invitationHandler: (Bool, MCSession!) -> Void) {