添加和获取NSMutableDictionary的值无法正常工作[重复](Adding and getting values of NSMutableDictionary not working [duplicate])

这个问题在这里已有答案:

NSMutableDictionary setObject:forKey:无法添加键 1答案

我正在尝试做一些复杂的事情,但是当我添加到一个nsmutabledictionary并尝试从中获取值时,它只是返回任何内容而不是在我尝试打印它返回时打印任何东西。 即使动作被触发,我甚至无法在ibaction中打印任何内容。 你能不能让我知道我做错了什么,它是什么? 谢谢!

我正在做的超简化版的示例代码:

NSMutableDictionary *test; [test setObject:@"ValueIWantToGet" forKey:@"KeyIAmSetting"]; NSLog([test objectForKey:@"KeyIAmSetting"]); //Should print "ValueIWantToGet", right?

This question already has an answer here:

NSMutableDictionary setObject:forKey: fails to add key 1 answer

I'm trying to do some complex stuff, but when I add to an nsmutabledictionary and try to get values from it, it's simply returning nothing and not printing anything when I try to print what it returns. I can't even get the following to print anything in an ibaction, even though the action is being triggered. Could you like me know if I'm doing anything wrong and what it is? Thanks!

Sample code of super simplified version of what I'm doing:

NSMutableDictionary *test; [test setObject:@"ValueIWantToGet" forKey:@"KeyIAmSetting"]; NSLog([test objectForKey:@"KeyIAmSetting"]); //Should print "ValueIWantToGet", right?

最满意答案

您的test变量未初始化。 改为:

NSMutableDictionary *test = [NSMutableDictionary dictionary];

要么

NSMutableDictionary *test = [[NSMutableDictionary alloc] init];

在Objective-C中,所有对象都通过指针进行操作。 因此,变量名前面的声明中有星号。 如果不为指针指定任何内容,则其值仍未初始化,因此对它的任何引用都是未定义的行为。 但是,在值恰好为nil情况下,您不会看到崩溃,因为Objective-C允许将消息发送到nil (它们没有效果)。

我正在创建NSMutableDictionary的属性,所以如果它是属性,我该如何初始化它?

例如,属性应该在类的初始值设定项中初始化

-(instancetype)init { if (self = [super init]) { _test = [NSMutableDictionary dictionary]; } return self; }

Your test variable is not initialized. Do this instead:

NSMutableDictionary *test = [NSMutableDictionary dictionary];

or

NSMutableDictionary *test = [[NSMutableDictionary alloc] init];

In Objective-C all objects are manipulated through pointers. Hence the asterisks in declarations in front of variable names. When you do not assign anything to a pointer, its value remains uninitialized, so any reference to it is undefined behavior. In situations when the value happens to be nil, however, you would not see a crash, because Objective-C allows sending messages to nil (they have no effect).

I'm making a property of an NSMutableDictionary, so how should I initialized it if it's a property?

Properties should be initialized in the initializer of your class, for example

-(instancetype)init { if (self = [super init]) { _test = [NSMutableDictionary dictionary]; } return self; }添加和获取NSMutableDictionary的值无法正常工作[重复](Adding and getting values of NSMutableDictionary not working [duplicate])

这个问题在这里已有答案:

NSMutableDictionary setObject:forKey:无法添加键 1答案

我正在尝试做一些复杂的事情,但是当我添加到一个nsmutabledictionary并尝试从中获取值时,它只是返回任何内容而不是在我尝试打印它返回时打印任何东西。 即使动作被触发,我甚至无法在ibaction中打印任何内容。 你能不能让我知道我做错了什么,它是什么? 谢谢!

我正在做的超简化版的示例代码:

NSMutableDictionary *test; [test setObject:@"ValueIWantToGet" forKey:@"KeyIAmSetting"]; NSLog([test objectForKey:@"KeyIAmSetting"]); //Should print "ValueIWantToGet", right?

This question already has an answer here:

NSMutableDictionary setObject:forKey: fails to add key 1 answer

I'm trying to do some complex stuff, but when I add to an nsmutabledictionary and try to get values from it, it's simply returning nothing and not printing anything when I try to print what it returns. I can't even get the following to print anything in an ibaction, even though the action is being triggered. Could you like me know if I'm doing anything wrong and what it is? Thanks!

Sample code of super simplified version of what I'm doing:

NSMutableDictionary *test; [test setObject:@"ValueIWantToGet" forKey:@"KeyIAmSetting"]; NSLog([test objectForKey:@"KeyIAmSetting"]); //Should print "ValueIWantToGet", right?

最满意答案

您的test变量未初始化。 改为:

NSMutableDictionary *test = [NSMutableDictionary dictionary];

要么

NSMutableDictionary *test = [[NSMutableDictionary alloc] init];

在Objective-C中,所有对象都通过指针进行操作。 因此,变量名前面的声明中有星号。 如果不为指针指定任何内容,则其值仍未初始化,因此对它的任何引用都是未定义的行为。 但是,在值恰好为nil情况下,您不会看到崩溃,因为Objective-C允许将消息发送到nil (它们没有效果)。

我正在创建NSMutableDictionary的属性,所以如果它是属性,我该如何初始化它?

例如,属性应该在类的初始值设定项中初始化

-(instancetype)init { if (self = [super init]) { _test = [NSMutableDictionary dictionary]; } return self; }

Your test variable is not initialized. Do this instead:

NSMutableDictionary *test = [NSMutableDictionary dictionary];

or

NSMutableDictionary *test = [[NSMutableDictionary alloc] init];

In Objective-C all objects are manipulated through pointers. Hence the asterisks in declarations in front of variable names. When you do not assign anything to a pointer, its value remains uninitialized, so any reference to it is undefined behavior. In situations when the value happens to be nil, however, you would not see a crash, because Objective-C allows sending messages to nil (they have no effect).

I'm making a property of an NSMutableDictionary, so how should I initialized it if it's a property?

Properties should be initialized in the initializer of your class, for example

-(instancetype)init { if (self = [super init]) { _test = [NSMutableDictionary dictionary]; } return self; }