将NSDictionary复制到NSMutableDictionary不起作用?

时间:2013-12-03 11:42:10

标签: ios iphone nsdictionary nsmutabledictionary

我试图将一个NSDictionary的内容复制到NSMutableDictionary中。 这是我到目前为止所做的。

 @implementation RosterListController
- (void)newMessageReceived:(NSDictionary *)messageContent 
{
   self.messageDictionary = [[NSMutableDictionary alloc]initWithDictionary:messageContent];         // This is where I am copying  

   UIApplication *app = [UIApplication sharedApplication];
   if (app.applicationState == UIApplicationStateActive)
   {
       UILocalNotification *localNotif = [[UILocalNotification alloc]init];
       if (localNotif)
      {
        localNotif.alertAction = @"OK";
        localNotif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
        [app presentLocalNotificationNow:localNotif];

      }
    }
}

然后我在appDelegate中处理此通知,方法是显示一个AlertView.After,完成后返回到RosterViewController。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
    NSLog(@"Cancel button presed");
}
else
{
    if(chatView)  // Chat ViewController
    {
        [chatView recvdMsg:self.messageDictionary];
    }
    else
    {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                                 bundle: nil];
         chatView=(ChatViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"chatviewcontroller"];

        NSMutableDictionary *buddy=[[NSMutableDictionary alloc]init];
        // The break point stops the app here & shows buddy dictionary as nil.
 ==>    [buddy setObject:[self.messageDictionary objectForKey:@"sender"] forKey:@"jid"];

   // Originally it was messageContent dict from the parameter above that I used which worked fine..
  //But since I need that dictionary in another method..I copied that dictionary into self.messageDictionary which also gets copied.
  // However now the above line causes problems.


      //  [buddy setObject:[self.messageContent objectForKey:@"sender"] forKey:@"jid"];
        chatView.buddyData=buddy;
        [self.navigationController pushViewController:chatView animated:YES];
        [chatView recvdMsg:self.messageDictionary];

    }
}
}

1 个答案:

答案 0 :(得分:0)

要从NSMutableDictionary创建NSDictionary,您应该使用 dictionaryWithDictionary 中声明的NSDictionary.h

NSMutableDictionary *aMutDictFromAnotherDictionary = [NSMutableDictionary dictionaryWithDictionary:YOUR_NSDICTIONARY_OBJECT];