2011-08-24 132 views
0
for(NSDictionary *feed in Feeds) 
{ 
    NSString *feedName=[feed objectForKey:@"name"]; 
           if(listofBusiness==nil) 
{ 
            listofBusiness=[[NSMutableArray alloc]init]; 
    } 
            if([listofBusiness indexOfObject:feedName] !=NSNotFound) 
     { 
     [listofBusiness addObject:feedName]; 
     [feedName release]; 
     feedName=nil; 
           } 

          } 
在此代碼

當編譯器都對這一說法這段代碼有什麼錯誤?

如果([listofBusiness indexOfObject:feedName]!= NSNotFound)

則不能進入codition走在遞增for循環以便任何元素不會添加到數組中。此代碼中的錯誤是什麼?

+1

無關你的問題具有很好的答案,在代碼中的內存管理是全亂了。運行靜態分析器。 – 2011-08-24 11:56:08

回答

3

的邏輯似乎倒 - 你可能希望它在

[listofBusiness indexOfObject:feedName] == NSNotFound 

但此刻你有相反的添加elemement - 你只嘗試添加對象時,它是「不能沒有找到' - 即它已經出現在列表中。

0

indexOfObject不適用於數組

嘗試使用數組的containsObject方法。

例子:

  if([listofBusiness containsObject:feedName]) { 
      // your code 
     }