2011-09-29 111 views
1

我正在實施搜索欄控制器來搜索表視圖。執行搜索的下面的方法代碼崩潰,出現錯誤「 - [__ NSArrayM rangeOfString:options:]:發送到實例0x65558e0的無法識別的選擇器'搜索欄控制器 - 搜索結果時崩潰

locationInfo數組是包含26個數組的數組,每個數組包含一個數字的由字符串的對象。

任何人都可以說明爲什麼該代碼崩潰?

謝謝。

- (void)handleSearchForTerm:(NSString *)searchTerm 
{ 
[self setSavedSearchTerm:searchTerm]; 

if ([self searchResults] == nil) 
{ 
    NSMutableArray *array = [[NSMutableArray alloc] init]; 
    [self setSearchResults:array]; 
    [array release], array = nil; 
} 

[[self searchResults] removeAllObjects]; 

if ([[self savedSearchTerm] length] != 0) 
{ 
    for (NSString *currentString in [self locationInfo]) 
    { 
     if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound) 
     { 
      [[self searchResults] addObject:currentString]; 
     } 
    } 
} 
} 
+0

東西展現代碼在哪裏設置選擇器 – Fredrik

回答

0

根據你得到的錯誤,似乎[self locationInfo]返回ARRA y(NSArray),而不是您期望的字符串(NSString)。

2

正如你在質詢說「locationInfo」是包含26個陣列, 所以, currentString在[自locationInfo]會返回一個數組只所以要儘量寫類似下面的數組:

爲(NSArray的* currentArray在[個體經營locationInfo])

{ 
for (NSString *currentString in currentArray) 
{ 
    if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound) 
    { 
     [[self searchResults] addObject:currentString]; 
    } 
} 

}

或類似這樣的