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

匆匆过客 提交于 2019-11-29 02:06:43

问题


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.

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?

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

回答1:


It wants an actual object... use NSNull

[NSNull null];



回答2:


You can set a nil value using setValue:forKey but it removes the key.

If you want to be able to set a key to nil you could use setValue:forKey: which will remove the key if you set it to nil (quote from documentation below). Note the Value instead of 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 nil in which case the method instead attempts to remove key using removeObjectForKey:.

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

Return value:

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

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




回答3:


You can set nil object in this way:

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"];



回答4:


When using this method:

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

Parameters (according to Apple doc's):

anObject:

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

aKey

Raises an invalidArgumentException if aKey is nil.




回答5:


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

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 .

to check just use

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


来源:https://stackoverflow.com/questions/12008365/ios-why-cant-i-set-nil-to-nsdictionary-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!