【问题标题】:Copying NSDictionary to NSMutable Dictionary doesn't work?将 NSDictionary 复制到 NSMutable Dictionary 不起作用?
【发布时间】:2013-12-03 11:42:10
【问题描述】:

我正在尝试将一个 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 来处理此通知。完成后..返回 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];

    }
}
}

【问题讨论】:

    标签: ios iphone nsdictionary nsmutabledictionary


    【解决方案1】:

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

    NSMutableDictionary *aMutDictFromAnotherDictionary = [NSMutableDictionary dictionaryWithDictionary:YOUR_NSDICTIONARY_OBJECT];
    

    【讨论】:

    • 试过你的方法,还是不行。好友词典显示0键/值对。
    • 是的.....实际上,当我“NSLog”它们时,“messageContent”和“messageDictionary”都显示相同的内容。
    • 您不想将messageContent的内容复制到messageDictionary吗? @codes
    • [buddy setObject:[messageContent objectForKey:@"sender"] forKey:@"jid"];..这曾经工作......但如果我使用 self.messageDictionary messageContent...复制后...不起作用。
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多