从ios中的nsmutableDictionary中删除一个键

时间:2013-09-11 06:55:01

标签: ios json nsdictionary

我有一个NSMutableDictionary,它以JSON格式从服务器返回。

从那里我必须删除一个空值而不使用的键。我必须删除键和值

我们如何删除该密钥......

关键值是

appointmentType = 1;
description = Right;
endDate = "2013-09-11";
endTime = "02:30:00";
flgHolidaySchedule = 1;
flgSiteSetupSchedule = 1;
therapistId = "maheswaran@excelgoodies.com";
uri = "<null>";

现在我必须从字典中删除uri Key ....

3 个答案:

答案 0 :(得分:0)

遍历字典并查找任何空条目并将其删除。

NSMutableDictionary *prunedDictionary = [NSMutableDictionary dictionary];
for (NSString * key in [yourDictionary allKeys])
{
    if (![[yourDictionary objectForKey:key] isKindOfClass:[NSNull class]])
        [prunedDictionary addObject:[yourDictionary objectForKey:key] forKey:key];
}

之后,prunedDictionary应该包含原始字典中的所有非空项。

答案 1 :(得分:0)

尝试使用以下代码:

NSMutableDictionary *myTemDictionary = [NSMutableDictionary dictionary];

for (NSString * key in [yourDictionaryFromJSON allKeys])
{
    if (![[yourDictionaryFromJSON objectForKey:key] isKindOfClass:[NSNull class]])
        [myTemDictionary addObject:[yourDictionary objectForKey:key] forKey:key];
}

NSLog(@"%@", myTemDictionary); // check it in console your dictionary has not any NULL value

答案 2 :(得分:0)

这很简单:

[yourDictionaryFromJSON removeObjectForKey:@"uri"];