Assigning nil to an element in NSMutableDictionary in Objective-C removes this element, is this an authorized function or an error?

I have one NSMutableDictionary.

NSMutableDictionary * dict = @{
                                @"0" : @"car",
                                @"1" : @"ball",
                                @"2" : @"plane",
}

At some point, by mistake, I assigned a zero to a dictionary element. For example:

  dict[@"1"] = nil; 

for my surprise, instead of crashing, element "1" is deleted.

Is this something recent? authorized function or error? I wonder if this is a feature because I always used something like

[dict removeObjectForKey:@"1"];

Removing objects from dictionaries.

I never knew this was possible. Perhaps Apple is making Objective-C look like Swift.

+4
source share
1 answer

I just tested the behavior in Swift 2.0.

var dict: NSMutableDictionary = [ "0" : "car", "1" : "ball", "2" : "plane" ];
dict["1"] = nil
print("\(dict)")

, .

NSMutableDictionary:

- setObject: forKeyedSubscript: - .

OBJECTIVE-C

- (void) setObject: (ObjectType)
forKeyedSubscript: (ID) AKEY

aKey. .

NSInvalidArgumentException, anObject nil. nil , NSNull.

. ( copyWithZone:; NSCopying). aKey , aObject .

NSInvalidArgumentException, aKey .


: 2016-04-21

Apple ! nil .

aKey. .

nil , , aKey, .

+5

Source: https://habr.com/ru/post/1608210/


All Articles