have been following this tutorial :
http://www.appcoda.com/intro-multipeer-connectivity-framework-ios-programming/
But have been converting it to work in Swift. Have come unstuck on the following however :
-(void)peerDidChangeStateWithNotification:(NSNotification *)notification
{
MCPeerID *peerID = [[notification userInfo] objectForKey:@"peerID"];
NSString *peerDisplayName = peerID.displayName;
MCSessionState state = [[[notification userInfo] objectForKey:@"state"] intValue];
It is the last line that is causing trouble, I have got this far :
func peerDidChangeStateWithNotification(notification: NSNotification)
{
if let userInfo : AnyObject = notification.userInfo? {
let peerID = userInfo["peerID"] as MCPeerID
let state = userInfo["state"] as MCSessionState
But cannot get the "state" entry into type MCSessionState, getting the error :
'AnyObject' is not convertible to 'MCSessionState'
Can anyone help ... ? Quite new to Swift so sorry if this is an obvious one ...
Many thanks, Franco.
update 9sep :
the code that sets up the userInfo object in the first place is as follows, as far as I know calling the toRaw() method should just mean it treats it as if it were storing an Int ... ? In which case I don't know why your original answer doesn't work ...
func session(session: MCSession!, peer peerID: MCPeerID!, didChangeState state: MCSessionState)
{
var dict = ["peerID": peerID, "state": state.toRaw()]
NSNotificationCenter.defaultCenter().postNotificationName("MCDidChangeStateNotification", object: nil, userInfo: dict)
}
update 2 9sep :
Got it ! The full working version of the converted function is as follows if anyone else is following that same tutorial. Many thanks for your help Edwin Vermeer :
func peerDidChangeStateWithNotification(notification: NSNotification)
{
if let userInfo : AnyObject = notification.userInfo?
{
let peerID = userInfo["peerID"] as MCPeerID
let peerDisplayName = peerID.displayName
let state: MCSessionState = MCSessionState.fromRaw(Int(userInfo["state"] as NSNumber)) as MCSessionState!
if (state != MCSessionState.Connecting)
{
if (state == MCSessionState.Connected)
{
arrayConnectedDevices.append(peerDisplayName)
}
else if (state == MCSessionState.NotConnected)
{
if (arrayConnectedDevices.count > 0)
{
if let i = find(arrayConnectedDevices, peerDisplayName)
{
arrayConnectedDevices.removeAtIndex(i)
}
}
}
}
}
}
You can use this to convert the int to the enum:
let state: MCSessionState = MCSessionState.fromRaw(Int(userInfo["state"] as NSNumber)) as MCSessionState!