【问题标题】:Trying to use NSNotification in a multipeer connectivity test app in Swift尝试在 Swift 的多点连接测试应用程序中使用 NSNotification
【发布时间】:2014-09-08 21:03:18
【问题描述】:

一直在关注本教程:

http://www.appcoda.com/intro-multipeer-connectivity-framework-ios-programming/

但一直在将其转换为在 Swift 中工作。但是,在以下问题上遇到了问题:

-(void)peerDidChangeStateWithNotification:(NSNotification *)notification
    {
        MCPeerID *peerID = [[notification userInfo] objectForKey:@"peerID"];
        NSString *peerDisplayName = peerID.displayName;
        MCSessionState state = [[[notification userInfo] objectForKey:@"state"] intValue];

这是造成麻烦的最后一行,我已经走到这一步了:

func peerDidChangeStateWithNotification(notification: NSNotification)
{
    if let userInfo : AnyObject = notification.userInfo? {
        let peerID = userInfo["peerID"] as MCPeerID
        let state = userInfo["state"] as MCSessionState

但无法将“状态”条目转换为 MCSessionState 类型,出现错误:

'AnyObject' is not convertible to 'MCSessionState'

任何人都可以帮助...?对 Swift 来说很新,如果这是一个明显的问题,很抱歉......

非常感谢,佛朗哥。


9sep 更新:

首先设置 userInfo 对象的代码如下,据我所知,调用 toRaw() 方法应该只是意味着它把它当作存储一个 Int ...?在这种情况下,我不知道为什么您的原始答案不起作用...

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)

}

更新 2 9sep:

知道了!如果其他人遵循相同的教程,则转换函数的完整工作版本如下。非常感谢您的帮助 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)
                    }
                }
            }
        }
    }
}

【问题讨论】:

    标签: xcode swift ios8 multipeer-connectivity mcsession


    【解决方案1】:

    您可以使用它来将 int 转换为 enum:

    let state: MCSessionState = MCSessionState.fromRaw(Int(userInfo["state"] as NSNumber)) as MCSessionState!
    

    【讨论】:

    • 嗨 Edwin 非常感谢您的快速回复,上面的问题是它现在给了我以下错误:'AnyObject' is not convertible to 'Int' - 我猜是因为我指的是userInfo 的方式来自notification ?
    • 啊,我以为你在 userinfo 中的 state 字段是一个 int 值。现在我看到您正在将其转换为 int。然后你应该在这里做同样的事情。你的状态字段是什么?它是一个字符串吗?然后只需添加 .toInt() 我为此更新了答案
    • 再次感谢 Edwin - 我认为 state 字段也是一个 int 值!请参阅上面的附加代码..
    • 好的,非常感谢@Edwin - 以下工作:let state: MCSessionState = MCSessionState.fromRaw(Int(userInfo["state"] as NSNumber)) as MCSessionState!
    • 啊!让状态:MCSessionState = MCSessionState.fromRaw(Int(userInfo["state"] as NSNumber)) as MCSessionState!这就是我现在不能处理 Swift 的原因。Swift 摇滚,但与 Obj-C 的向后兼容性很难处理......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多