JabberClient:收到消息后应用程序崩溃

时间:2015-07-29 17:46:11

标签: xmppframework

我正在关注适用于iOS的JabberClient的相当古老的教程。到目前为止工作得很好。 我可以向其他用户发送消息,但只要有人给我写了一条消息,该应用就会崩溃并显示以下消息。

*由于未捕获的异常终止应用' NSInvalidArgumentException',原因:' * setObjectForKey:object不能为nil(key:msg)'

以下是接收消息的方法的代码。

 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{

    NSString *msg = [[message elementForName:@"body"] stringValue];
    NSString *from = [[message attributeForName:@"from"] stringValue];

    NSMutableDictionary *m = [[NSMutableDictionary alloc] init];

    [m setObject:msg forKey:@"msg"];
    [m setObject:from forKey:@"sender"];

    [_messageDelegate newMessageReceived:m];
}

问题似乎是XMPP框架的elementForName:方法。请参阅该方法的定义和注释。

- (NSXMLElement *)elementForName:(NSString *)name
{
    NSArray *elements = [self elementsForName:name];
    if ([elements count] > 0)
    {
        return elements[0];
    }
    else
    {
        // There is a bug in the NSXMLElement elementsForName: method.
        // Consider the following XML fragment:
        //
        // <query xmlns="jabber:iq:private">
        // <x xmlns="some:other:namespace"></x>
        // </query>
        //
        // Calling [query elementsForName:@"x"] results in an empty array!
        //
        // However, it will work properly if you use the following:
        // [query elementsForLocalName:@"x" URI:@"some:other:namespace"]
        //
        // The trouble with this is that we may not always know the xmlns in advance,
        // so in this particular case there is no way to access the element without looping through the children.
        //
        // This bug was submitted to apple on June 1st, 2007 and was classified as "serious".
        //
        // --!!-- This bug does NOT exist in DDXML --!!--
        return nil;
    }
}

我不太明白他们在那里发布的XML片段。 有没有人有类似的问题或者知道如何解决这个问题?

0 个答案:

没有答案