How to remove key from nsmutabledictionary When value of that key is empty or null in ios?
Asked Answered
D

3

8

I have created NSMutableDictionary and the values of keys are formed as nsarray. I have removed objects of keys. I have no doubt for this case. But I would like to remove key When value of that key is empty or null as following as the case.

{
    Africa =     (
        "Kenya",
        "South Africa"
    );
    America =     (
    );
    Asia =     (
        "India"
    );
}

The above dictionary has three keys and one of these keys has no objects that is America. I would like remove this key. Is it possible? Can we remove key from nsmutabledictionary?

Please help me.....

Edited: Simple mistake. I understand what i have mistaken. The key(America) has value and which is empty array that means the number of elements in array is zero.

Demobilize answered 29/1, 2013 at 7:6 Comment(2)
[dict removeObjectForKey:@"America"];Freddyfredek
check out #3880046Catenate
T
30

The above dictionary has three keys and one of these keys has no objects that is America.

FALSE. It holds an empty NSArray. This is how you remove it:

[dictionary removeObjectForKey:@"America"];
Trieste answered 29/1, 2013 at 7:9 Comment(5)
@AKV Thank you :) +1 for you too.Trieste
@PrasadG : whenever you see { } that is dictionary, & ( ) are arrays... remember :)Wismar
@AKV Also, he needs to remember that there's no way an NSDictionary can hold nil. (Let's not talk about CoreFoundation trickery for now...)Trieste
@AKV Yes, I saw that, and that's why you got +1 from me :-)Trieste
Thanks Dwight! :)Reggy
W
3

How to remove key from nsmutabledictionary When value of that key is empty or null in ios?

This is not possible, dictionary must have Key and Value a non-nil Value.

In your case it is Array. that contains no value.

EDIT:

Whenever you see { }, that is dictionary, & ( ) is array.

Wismar answered 29/1, 2013 at 7:14 Comment(1)
no problem, sometimes it is allowed, not always, Ek Galti Maaf :)Wismar
T
0

in swift 3.0

dictionary.removeObject(forKey: "America")
Tuckie answered 2/3, 2017 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.