用通知中心传值时候的注意事项

 当用通知中心 去传值的时候  通常 传的 都是字典 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

     NSDictionary *dic=[defaults objectForKey:@"forcedUpdateResponseObject2"];

        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

        [nc  postNotificationName:forcedUpdateGameVersion object:nil userInfo:dic];

然后 在 appdelegate中设置 注册通知的方法 接收 传递的字典

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(forcedUpdateNotice:) name:forcedUpdateGameVersion object:nil];

    return YES;

}

-(void)forcedUpdateNotice:(NSNotification *)text

{

//获取值得时候 不能直接用text.userInfo[@"forcedUpdateResponseObject2"] 来获取 

//这样获取到得值 是nil的  要想得到字典的值  直接 text.userInfo[@"data"],text.userInfo[@"errorCode"] 其中 data ,errorCode  都是forcedUpdateResponseObject2字典中键  

    NSLog(@"测试token的通知data=%@ errorCode= %@errorMessage  %@",text.userInfo[@"data"],text.userInfo[@"errorCode"],text.userInfo);

    NSDictionary *dic=text.userInfo[@"forcedUpdateResponseObject2"];

  int errorCode=[text.userInfo[@"errorCode"]intValue];

    NSLog(@"!!!!!!%@",dic);

 //最 最 最 需要注意的 :

//有时候 你如果 直接 text.userInfo[@"errorCode"]这样 获取 字典中得某个值的时候 你还是获取不到(前提是 你确定传值了),明明传值了 为什么errorCode还是获取不到值呢

然后 我就打印nslog 下  发现 这时候errorCode 才有值了

就好像 nslog 先声明了一下似的  这个 bug  目前 只能这样处理下  至于 原因 我目前还没找到 可能有两种猜测 一个是我们后台的原因 一个 就是Xcode的一个bug

}

原文地址:https://www.cnblogs.com/pp-pping/p/4409257.html