如何检查Iphone中是否存在字典?

时间:2012-07-19 15:26:01

标签: iphone ios ipad

我有一个Iphone应用程序,我正在从一个字典中添加一个数组的值。任何人都告诉我如何在迭代之前检查字典是否存在?

NSDictionary *news=[dict objectForKey:@"news"];
NSDictionary *deals=[dict objectForKey:@"deals"];
NSLog(@"%@",[news classForCoder]);
NSLog(@"%@",news);
NSLog(@"%@",deals);

for(NSDictionary *key in news)
{
    if([key isKindOfClass:[NSDictionary class]])
    {        
        [dataArray addObject:key]; 
    }
    // NSLog(@"%@",rewardsId);
    //[dataArray addObject:dictrewards];
}

for(NSDictionary *key in deals)
{      
    if([key isKindOfClass:[NSDictionary class]])
    {  
        [dataArray1 addObject:key];       
    }

    // NSLog(@"%@",rewardsId);
    //[dataArray addObject:dictrewards];
}

self.newsseperatearray=dataArray;
self.dealsseperatearray=dataArray1;

self.newssarray = [[dataArray arrayByAddingObjectsFromArray:dataArray1] mutableCopy];

NSLog(@"%@",self.newssarray);
//self.newssarray=dataArray;
[self.mTableView reloadData];

1 个答案:

答案 0 :(得分:1)

你应该检查它是不是nil并且它实际上是NSDictionary

if(news && [news isKindOfClass:[NSDictionary class]]) {
    do stuff
}