简体   繁体   中英

NSDictionary key/value Concept

if the key does exist and the following code is executed ll it remove the object and set value for key to nil or it ll delete the key as well.

[myDetails removeObjectForKey:@"Name"];

What happens if the key is not present already? Will it generate exception/error?

Firstly you mean NSMutableDictionary , not NSDictionary , which is immutable and if the key does exist it will remove both the key and value. If the key does not exist, nothing will happen (no exception will be raised).

From the docs :

Discussion

Does nothing if aKey does not exist.

Does nothing if aKey does not exist.

From Apple documentation

Why don't you read the documentation before ask?

It will remove the Objects of that key and Key too.

Here is output of NSMutableDictionary before executing [getDataInSideBySide removeObjectForKey:@"1"];

getDataInSideBySide=={
    1 =     (
        "iProgress23807pm.png",
        "iProgress23822pm.png",
        "iProgress55154pm.png",
        "iProgress65626pm.png",
        "iProgress65639pm.png",
        "iProgress125224pm.png"
    );
    2 =     (
        "iProgress125217pm.png"
    );

and after executing [getDataInSideBySide removeObjectForKey:@"1"] ;

getDataInSideBySide=={
    2 =     (
        "iProgress125217pm.png"
    );
}

and when I execute [getDataInSideBySide removeObjectForKey:@"1"]; again, output is same

getDataInSideBySide=={
        2 =     (
            "iProgress125217pm.png"
        );
    }

It means it does nothing to the NSMutableDictionary .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM