简体   繁体   English

为什么存储在NSMutableDictionary中的方法参数中传递的值变得空

[英]Why value passed in method parameter is getting null when storing in NSMutableDictionary

- (void) msg:(NSString *)msg from:(NSString *)from
{
     NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
     [m setObject:msg forKey:@"msg"];
     [m setObject:from forKey:@"sender"];
     [m setObject:[NSString getCurrentTime] forKey:@"time"];


NSLog(@"MSG=>%@ : FROM=>%@ : TIME=>%@",[m objectForKey:msg], [m objectForKey:@"sender"], [m objectForKey:@"time"]);

NSLog(@"MSG=>%@",msg);

}

Reply of first log is 第一个日志的回复是

MSG=>(null) : FROM=>your friend! MSG =>(空):FROM =>您的朋友! : TIME=>Apr 30, 2012 12:52:02 AM :TIME => 2012年4月30日上午12:52:02

And reply of second log is 而第二条日志的回复是

MSG=>hello...how are you MSG =>你好...你好吗

I didn't understand why "msg" after going inside NSMutableDictionary getting null? 我不明白为什么在进入NSMutableDictionary之后获取“ msg”为空? after all parameter "from" is also displaying fine. 毕竟所有参数“ from”也显示正常。 And the interesting thing is logging msg parameter directly displaying perfectly. 有趣的是,记录的msg参数直接完美显示。

I am using ARC enabled project and getting this null issue. 我正在使用启用了ARC的项目,并得到此null问题。 I don't want to declare NSMutableDictionary out side of this method because it is getting called many times and every time new NSMutableDictionary created and stored in NSArray for further use. 我不想在此方法之外声明NSMutableDictionary,因为每次新的NSMutableDictionary创建并存储在NSArray中以供进一步使用时,都会多次调用它。

[m objectForKey:msg]

is wrong, you're trying to get back the object using itself as the key. 是错误的,您正在尝试使用自身作为键来取回对象。 Correctly: 正确地:

[m objectForKey:@"msg"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM