向nsdictionary添加键值会丢失键

时间:2022-04-16 13:43:28

I have been adding key value pairs to nsdictionary like this

我一直在向nsdictionary添加键值对,就像这样

NSDictionary *dict = @{key1:value1, key2:value2};

if value1 is not found, the app crashes. So, I implemented the other way of doing it

如果找不到value1,应用程序崩溃。所以,我实现了另一种方式

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:value1,key1,value2,key2,nil];

In this case, if value1 is not found, dict does not go further and does not have key2. What is the solution to this?

在这种情况下,如果找不到value1,则dict不会更进一步,并且没有key2。这是什么解决方案?

2 个解决方案

#1


2  

NSDictionary (as well as NSArray) cannot store nil, because they expect an object.

NSDictionary(以及NSArray)不能存储nil,因为它们期望一个对象。

A solution would be to use NSNull or to not store the value at all. In the later case the objectForKey: method will return nil if it does not find the value for the given key, which might be want you want.

解决方案是使用NSNull或根本不存储值。在后一种情况下,如果objectForKey:方法没有找到给定键的值,则它将返回nil,这可能是您想要的。

In the second code example the problem that it is not going further is that this method expects an nil-terminated list so it just stops at the first nil as it thinks this is the end.

在第二个代码示例中,它不会进一步的问题是此方法需要一个nil终止列表,因此它只是在第一个nil处停止,因为它认为这是结束。

You can use setValue:forKey:, this will just remove the value for the key if the value is nil. See here. But therefore it need to be a mutable array and you have to go through all values one by one.

您可以使用setValue:forKey :,如果值为nil,则只删除键的值。看这里。但因此它需要是一个可变数组,你必须逐个查看所有值。

Another solution would be to guard each value with a check, e.g. as shown here.

另一种解决方案是用支票来保护每个值,例如:如图所示。

#2


-1  

I have found the solution

我找到了解决方案

 NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
   [dict2 setValue:value1 forKey:key1];
   [dict2 setValue:value2 forKey:key2];
   [dict2 setValue:value3 forKey:key3];

    NSLog(@"dict2 - %@",dict2);

Works like a charm !!!

奇迹般有效 !!!

#1


2  

NSDictionary (as well as NSArray) cannot store nil, because they expect an object.

NSDictionary(以及NSArray)不能存储nil,因为它们期望一个对象。

A solution would be to use NSNull or to not store the value at all. In the later case the objectForKey: method will return nil if it does not find the value for the given key, which might be want you want.

解决方案是使用NSNull或根本不存储值。在后一种情况下,如果objectForKey:方法没有找到给定键的值,则它将返回nil,这可能是您想要的。

In the second code example the problem that it is not going further is that this method expects an nil-terminated list so it just stops at the first nil as it thinks this is the end.

在第二个代码示例中,它不会进一步的问题是此方法需要一个nil终止列表,因此它只是在第一个nil处停止,因为它认为这是结束。

You can use setValue:forKey:, this will just remove the value for the key if the value is nil. See here. But therefore it need to be a mutable array and you have to go through all values one by one.

您可以使用setValue:forKey :,如果值为nil,则只删除键的值。看这里。但因此它需要是一个可变数组,你必须逐个查看所有值。

Another solution would be to guard each value with a check, e.g. as shown here.

另一种解决方案是用支票来保护每个值,例如:如图所示。

#2


-1  

I have found the solution

我找到了解决方案

 NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
   [dict2 setValue:value1 forKey:key1];
   [dict2 setValue:value2 forKey:key2];
   [dict2 setValue:value3 forKey:key3];

    NSLog(@"dict2 - %@",dict2);

Works like a charm !!!

奇迹般有效 !!!