我对Objective C很陌生,我遇到了一段时间的麻烦,我一直在寻找答案,观看教程,但我一直无法找到解决问题的方法。也许它就在我面前而我没有看到它......我认为它与我的 UISearch Bar 的 NSRange 有关。但我不是 100% 确定。
我正在尝试搜索具有我的国家/地区字典键的数组,我已经设置了几个 NSLog,我看到这些键正在进入我设置的数组以保存它们,然后我得到下面的错误.. .
因此,据我了解,它计算的是有 5 个的 countryKeys,所以索引 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