gpt4 book ai didi

ios - 为什么在方法参数中传递的值在存储在 NSMutableDictionary 中时会变为 null

转载 作者:行者123 更新时间:2023-11-28 20:36:23 25 4
gpt4 key购买 nike

- (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);

}

第一个日志的回复是

MSG=>(null) : FROM=>your friend! : TIME=>Apr 30, 2012 12:52:02 AM

第二个日志的回复是

MSG=>hello...how are you

我不明白为什么“msg”在进入 NSMutableDictionary 后变为 null?毕竟参数“from”也显示正常。有趣的是logging msg参数直接完美显示。

我正在使用启用 ARC 的项目并遇到此空问题。我不想在此方法之外声明 NSMutableDictionary,因为它被调用了很多次,并且每次都会调用新的 NSMutableDictionary 并将其存储在 NSArray 中以供进一步使用。

最佳答案

[m objectForKey:msg]

是错误的,您正试图取回使用自身作为键的对象。正确:

[m objectForKey:@"msg"]

关于ios - 为什么在方法参数中传递的值在存储在 NSMutableDictionary 中时会变为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10375600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com