I am trying to write this code in Swift 3, it is for multiplayer -
this code in old version of swift 2. trying write in swift 3 having problem. multiplayer game. having problem "let userinfo ". error "extra argument 'userinfo' "in call
import uikit import multipeerconnectivity class mpchander: nsobject, mcsessiondelegate { var peerid:mcpeerid! var session:mcsession! var browser:mcbrowserviewcontroller! var advertiser:mcadvertiserassistant? = nil func setuppeerwithdisplayname (displayname:string){ peerid = mcpeerid(displayname: displayname) } func setupsession(){ session = mcsession(peer: peerid) session.delegate = self } func setupbrowser(){ browser = mcbrowserviewcontroller(servicetype: "my-game", session: session) } func advertiseself(advertise:bool){ if advertise{ advertiser = mcadvertiserassistant(servicetype: "my-game", discoveryinfo: nil, session: session) advertiser!.start() }else{ advertiser!.stop() advertiser = nil } } func session(_ session: mcsession, peer peerid: mcpeerid, didchange state: mcsessionstate) { let userinfo = ["peerid":peerid,"state":state.rawvalue] dispatch_async(dispatch_get_main_queue(), { () -> void in nsnotificationcenter.defaultcenter().postnotificationname("mpc_didchangestatenotification", object: nil, userinfo: userinfo) }) } func session(_ session: mcsession, didreceive data: data, frompeer peerid: mcpeerid) { let userinfo = [data:"data" , peerid:"peerid"] [anyhashable : string] notificationcenter.default.post(name: nsnotification.name(rawvalue: "mpc_didchangestatenotification"), object: nil, userinfo: userinfo) } func session(_ session: mcsession, didfinishreceivingresourcewithname resourcename: string, frompeer peerid: mcpeerid, @ localurl: url?, witherror error: error?) { } func session(_ session: mcsession, didstartreceivingresourcewithname resourcename: string, frompeer peerid: mcpeerid, progress: progress) { } func session(_ session: mcsession, didreceive stream: inputstream, withname streamname: string, frompeer peerid: mcpeerid) { } }
you should've posted entire console output.
the problem nsnotificationcenter. swap func session(_:, peer:, didchange:)
method with:
func session(_ session: mcsession, peer peerid: mcpeerid, didchange state: mcsessionstate) { let userinfo = ["peerid":peerid,"state":state.rawvalue] as! [anyhashable : string] dispatchqueue.main.async { notificationcenter.default.post(name: nsnotification.name(rawvalue: "mpc_didchangestatenotification"), object: nil, userinfo: userinfo) } }
Comments
Post a Comment