0

我有一个 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];
4

1 回答 1

1

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

if(news && [news isKindOfClass:[NSDictionary class]]) {
    do stuff
}
于 2012-07-19T15:30:35.590 回答