2013-03-20 55 views
0

我很新的Objective C和我一直有麻煩了一段時間,我一直在尋找答案,看教程,我一直沒能找到解決方案對我的問題。也許它在我面前的權利,我沒有看到它......我認爲這與我的UISearch酒吧的NSRange有關。但我不是100%肯定的。NSRangeException錯誤... NSDictionary,NSArray,UISearchBar

我想通過具有我的國家字典的關鍵字的數組進行搜索,我已經設置了幾個NSLogs,並且我看到這些關鍵字將進入我已經設置保存它們的數組中,然後我得到以下錯誤...

因此,據我瞭解,這是計數countryKeys有5,所以索引4應該存在,不應該?但它然後顯示0 .. 3,所以我這就是爲什麼我認爲我錯誤地使用NSRange。到目前爲止,我已經看過一些UISearchBar和For循環教程,我很瞭解For循環和它們更好,我想我需要弄清楚NSRange。

讓我知道如果你不明白我的思維過程或任何東西,我提到..

Filtered Countries (
     ATE, 
     ARE, 
     USA, 
     JAP, 
     CAN 
    ) 
    *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 4 beyond bounds [0 .. 3]' 

-

#import "ViewController.h" 
@interface ViewController() 
@end 

@implementation ViewController 

@synthesize dictTableView, mySearchBar, countriesInRegion, filteredCountries, isFiltered, regionCodes, countryKey, countryKeys, countryInfo, sortedKeysArray, regionCode, dict1; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    dict1 = [[NSMutableDictionary alloc] init]; 
    NSMutableDictionary *subDict = [[NSMutableDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"United States",@"US",@"USA", @"NAC", nil] 
                     forKeys: [NSArray arrayWithObjects:@"countryName", @"countryAbbrev", @"countryID", @"countryRegion", nil]]; 
    [dict1 setObject:subDict forKey:@"USA"]; 

    subDict = [[NSMutableDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Canada", @"CA", @"CAN", @"NAC", nil] 
                forKeys: [NSArray arrayWithObjects:@"countryName", @"countryAbbrev", @"countryID", @"countryRegion", nil]]; 
    [dict1 setObject:subDict forKey:@"CAN"]; 

    subDict = [[NSMutableDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Japan", @"JP", @"JAP", @"EAS", nil] 
                forKeys: [NSArray arrayWithObjects:@"countryName", @"countryAbbrev", @"countryID", @"countryRegion", nil]]; 
    [dict1 setObject:subDict forKey:@"JAP"]; 

    subDict = [[NSMutableDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"United Arab Emirates", @"AE", @"ARE", @"MEA", nil] 
                forKeys: [NSArray arrayWithObjects:@"countryName", @"countryAbbrev", @"countryID", @"countryRegion", nil]]; 
    [dict1 setObject:subDict forKey:@"ARE"]; 

    subDict = [[NSMutableDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Antigua and Barbuda", @"AG", @"ATE", @"LCN", nil] 
                forKeys: [NSArray arrayWithObjects:@"countryName", @"countryAbbrev", @"countryID", @"countryRegion", nil]]; 
    [dict1 setObject:subDict forKey:@"ATE"]; 

    regionCodes = [[NSMutableDictionary alloc] init]; 

    countryKeys = [dict1 allKeys]; 
    NSLog(@"countryKeys %@", countryKeys); 

    sortedKeysArray = [countryKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; 

    for (int i = 0; i < [sortedKeysArray count]; i++) { 
     countryKey = [sortedKeysArray objectAtIndex:i]; 
     countryInfo = [dict1 objectForKey:countryKey]; // NSLog(@"returning countryKey to countryInfo %@", countryInfo); 
     regionCode = [countryInfo objectForKey:@"countryRegion"]; // NSLog(@" adding countryRegion Key to regionCode %@", regionCode); 
     // NSLog(@"the contents of countryInfo is %@", countryInfo); 

     if (![regionCodes objectForKey:regionCode]) { 
      countriesInRegion = [[NSMutableArray alloc] init]; 
      [regionCodes setObject:countriesInRegion forKey:regionCode]; 

      [countriesInRegion addObject:countryInfo]; // NSLog(@"if adding countryInfo to initialCountries %@", countryInfo); 

     } 
     else{ 
      countriesInRegion = [regionCodes objectForKey:regionCode]; 
      [countriesInRegion addObject:countryInfo]; // NSLog(@"else adding countryInfo to initialCountries %@", countryInfo); 
     } 
    } 

} 



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if (isFiltered == NO) 
    { 
     return countryInfo.count; 
    } 
    else 
    { 
     return filteredCountries.count; 
    } 

} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

{ 
    NSArray *sortKey = [regionCodes allKeys]; 
    sortedKeysArray = [sortKey sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; 
    return [sortedKeysArray objectAtIndex:section]; 

} 


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 
{ 
    NSString *regionKey = [[regionCodes allKeys] objectAtIndex:section]; 
    countriesInRegion = [regionCodes objectForKey:regionKey]; 

    if (isFiltered == NO) 
    { 
     return countriesInRegion.count; 
    } 
    else 
    { 
     return filteredCountries.count; 
    } 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    UITableViewCell *cell = nil; 

    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

    if (cell == nil) { 

     cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"Cell"]; 

     UILabel *countryNameLabel = [[UILabel alloc] initWithFrame:(CGRectMake(10, 0, 220, 44))]; 
     [countryNameLabel setBackgroundColor:[UIColor clearColor]]; 

     UILabel *countryAbbrevLabel = [[UILabel alloc] initWithFrame:(CGRectMake(230, 0, 75, 44))]; 
     [countryNameLabel setBackgroundColor:[UIColor clearColor]]; 

     UILabel *countryIDLabel = [[UILabel alloc] initWithFrame:(CGRectMake(275, 0, 50, 44))]; 
     [countryNameLabel setBackgroundColor:[UIColor clearColor]]; 

     [cell.contentView addSubview:countryNameLabel]; 
     [cell.contentView addSubview:countryAbbrevLabel]; 
     [cell.contentView addSubview:countryIDLabel]; 
    } 
    NSArray *regionKeys = [regionCodes allKeys]; 
    NSString *regionKey = [regionKeys objectAtIndex:indexPath.section]; 
    countriesInRegion = [regionCodes objectForKey:regionKey]; 
    NSLog(@"Countries in Region %@", countriesInRegion); 
    countryInfo = [countriesInRegion objectAtIndex:indexPath.row]; 

    NSArray *subviews = [cell.contentView subviews]; 
    UILabel *nameLabel = [subviews objectAtIndex:0]; 
    UILabel *abbrevLabel = [subviews objectAtIndex:1]; 
    UILabel *idLabel = [subviews objectAtIndex:2]; 
// NSLog(@"6 - Countries in Region %@", countriesInRegion); 

    if (isFiltered == NO) 
    { 
     nameLabel.text = [countryInfo objectForKey:@"countryName"]; 
     abbrevLabel.text = [countryInfo objectForKey:@"countryAbbrev"]; 
     idLabel.text = [countryInfo objectForKey:@"countryID"]; 
//  NSLog(@"5 - Our list of countries: %@", nameLabel.text); 
//  NSLog(@"Country Info %@",countryInfo); 

    } 
    else 
    { 
     nameLabel.text = [filteredCountries objectAtIndex:2]; 
     abbrevLabel.text = [filteredCountries objectAtIndex:1]; 
     idLabel.text = [filteredCountries objectAtIndex:0]; 

     NSLog(@"4 - Here are your results: %@", filteredCountries); 

    } 
    return cell; 
} 

#pragma mark - UISearchBarDelegate Methods 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 

    if (searchText.length == 0) { 
     isFiltered = NO; 

    } 
    else 
    { 
     isFiltered = YES; 
     NSLog(@"You entered %@", searchText); 
     filteredCountries = [[NSMutableArray alloc] init]; 
     countryKeys = [dict1 allKeys]; 
     NSLog(@"countryKeys %@", countryKeys); 

     for (int i = 0; i < countryKeys.count; i++) { 
      countryKey = [countryKeys objectAtIndex:i]; 
      NSLog(@"country key is %@", countryKey); 
      { 
       NSRange countryNameRange = [countryKey rangeOfString:searchText options:NSCaseInsensitiveSearch]; 


       if (countryNameRange.location !=NSNotFound) 
       { 
        [filteredCountries addObject:countryKey]; 

        NSLog(@"Filtered Countries %@", filteredCountries); 
       } 
      } 
     } 

    } 
    [dictTableView reloadData]; 
} 





- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 
    [mySearchBar resignFirstResponder]; 

} 




- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 


@end 

回答

0

該錯誤只是意味着你超過了必然的陣列。

具體來說,第一眼看來,我認爲你錯誤的部分數量,它不匹配數組中的元素數量。

我注意到,當filtered等於YES您將返回filteredCountries.count的行數和段數。

它看起來不正確,我認爲你應該仔細檢查你的索引。

+0

是的,事實證明我有回國的數量是部分的數量。所以它正在崩潰,因爲我有5個國家,只有四個地區。 – GunplaGamer 2013-03-26 22:19:53