0
我有一個Iphone應用程序,我正在從字典中向數組添加值。任何人告訴我如何在迭代之前檢查字典是否存在?如何檢查字典在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];
命名字典「鑰匙」是混亂!另外,你的代碼有太多的新行。 – 2012-07-19 15:28:52
是'news'數組的'NSDictionary'實例嗎? – 2012-07-19 15:30:03
你爲什麼要檢查它?如果它不存在(又名'nil'),迭代將不會啓動,並且不會對您造成任何問題。但爲了你的緣故,在迭代之前你可以檢查指針是有效的還是'nil'。 – holex 2012-07-19 15:34:36