iOS:为什么我不能将 nil 设置为 NSDictionary 值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12008365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:46:15  来源:igfitidea点击:

iOS: Why can't I set nil to NSDictionary value?

iphoneiosnsdictionarynull

提问by applefreak

This might be very basic question but I was wondering why can't I assign nil as NSDictionary value? I have following statement many places in my code. If [q objectForKey:@"text"]is nil then App is crashing.

这可能是非常基本的问题,但我想知道为什么我不能将 nil 指定为 NSDictionary 值?我的代码中有很多地方都有以下语句。如果[q objectForKey:@"text"]为零,则应用程序崩溃。

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:2];
[dict setObject:[q objectForKey:@"text"] forKey:@"text"];

I have to check everywhere for the nil before assigning it to dictionary. Is this the only correct way of doing? Am I missing something obvious?

在将其分配给字典之前,我必须到处检查 nil。这是唯一正确的做法吗?我错过了一些明显的东西吗?

if([q objectForKey:@"text"] != nil)
    [dict setObject:[q objectForKey:@"text"] forKey:@"text"];
else
    [dict setObject:@"" forKey:@"text"];

回答by tybro0103

It wants an actual object... use NSNull

它想要一个实际的对象...使用NSNull

[NSNull null];

回答by David R?nnqvist

You canset a nil value using setValue:forKeybut it removes the key.

可以使用设置一个 nil 值,setValue:forKey但它会删除键。

If you want to be able to set a key to nilyou could use setValue:forKey:which will remove the key if you set it to nil(quote from documentation below). Note the Valueinstead of Object.

如果您希望能够设置一个密钥,nil您可以使用setValue:forKey:它,如果您将其设置为nil(引用以下文档)将删除该密钥。注意Value而不是 Object。

setValue:forKey:

Adds a given key-value pair to the dictionary.

...
Discussion

This method adds value and key to the dictionary using setObject:forKey:, unless value is nilin which case the method instead attempts to remove key using removeObjectForKey:.

setValue:forKey:

将给定的键值对添加到字典中。

...
讨论

此方法使用 将值和键添加到字典中setObject:forKey:,除非值是nil这种情况,否则该方法会尝试使用 删除键removeObjectForKey:

When you later try and get the object using objectForKey:for the key that you removed by setting it to nilyou will get nilback (quote from documentation below).

当您稍后尝试objectForKey:通过将其设置为nil您删除的键来获取对象时,您将nil返回(引用自下面的文档)。

Return value:

The value associated with aKey, or nil if no value is associated with aKey.

返回值:

与 aKey 关联的值,如果没有与 aKey 关联的值,则为 nil。

Note: The key will not actually be present in the dictionary so it won't be obtained using allKeys; or be enumerated over.

注意:该键实际上不会出现在字典中,因此不会使用allKeys;获取它。或一一列举。

回答by SamSol

You can set nil object in this way:

您可以通过这种方式设置 nil 对象:

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

dictionary[@“key”] = nil;

Have you noticed it?

你注意到了吗?

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

/* this statement is safe to execute    */

dictionary[@“key”] = nil;

/* but this statement will crash application    */

[dictionary setObject:nil forKey:@"key"];

回答by Avi Levin

When using this method:

使用此方法时:

func setObject(_ anObject: Any, forKey aKey: NSCopying)

Parameters (according to Apple doc's):

参数(根据 Apple 文档):

anObject:

对象

Raises an invalidArgumentException if anObject is nil. If you need to represent a nil value in the dictionary, use NSNull .

如果 anObject 为 nil,则引发 invalidArgumentException。如果需要在字典中表示一个 nil 值,请使用 NSNull 。

aKey

关键

Raises an invalidArgumentException if aKey is nil.

如果 aKey 为零,则引发 invalidArgumentException。

回答by Kunal Balani

My friend using nil as marker is a sign of bad programming . nil is reserved for some diffrent purpose .

我的朋友使用 nil 作为标记是编程不良的标志。nil 保留用于某些不同的目的。

if([q objectForKey:@"text"] != nil)
    [dict setObject:[q objectForKey:@"text"] forKey:@"text"];
else
    [dict removeObjectforKey:@"text"]; // this will do nothing if key does not exsist.

//by default for all the keys the value is nil and you are trying to override this behavior. going against the language rules will always get you in trouble .

//默认情况下,所有键的值都是 nil 并且您正在尝试覆盖此行为。违反语言规则总是会给你带来麻烦。

to check just use

检查只是使用

if([dict objectforKey:@"text"] !=nil){} // this will work becuase default value is nil 
itself