当Xcode不断抛出nsexception时,如何按字母顺序显示单词

时间:2015-02-07 21:41:39

标签: ios objective-c xcode uitableview

我有以下代码。它导致了一个分段的uitableview。但是,每个部分中的单词顺序相反(在a之前的r,在t之前的x)。我曾尝试在代码的各个部分使用nsdescriptor和选择器,但它导致Xcode抛出nsexception ncsf字典无效选择器发送localisedcaseinsensitivecompare。这种情况只有在我提到上述情况时才会发生 - 即使我没有添加localisedCaseInsensitiveCompare(它适用于那些已经存在的实例)。

任何新鲜的眼睛都能解决这个问题。 感谢

@implementation RCViewController

static NSString *CellIdentifier = @"Cell Identifier";

@synthesize words;
@synthesize alphabetizedWords;
@synthesize wordDictionary;
@synthesize keys;


-(NSDictionary *)alphabetizedWords:(NSArray *)wordsArray {
    NSMutableDictionary *buffer = [[NSMutableDictionary alloc]init];

    for (int i=0; i <wordsArray.count; i++) {

        NSDictionary *keyValue = [wordsArray objectAtIndex:i];
        NSString *word = [[wordsArray objectAtIndex:i]objectForKey:@"Word"];
        NSString *firstLetter = [[word substringToIndex:1]uppercaseString];

        if ([buffer objectForKey:firstLetter]) {
            [(NSMutableArray *)[buffer objectForKey:firstLetter]addObject:keyValue];
        }
        else {
            NSMutableArray *mutableArray = [[NSMutableArray alloc]initWithObjects:keyValue, nil];
            [buffer setObject:mutableArray forKey:firstLetter];
        }
    }

    NSArray *bufferKeys = [buffer allKeys];
    for (int j; j<bufferKeys.count; j++) {
        NSString *bufferkey = [bufferKeys objectAtIndex:j];
        [(NSMutableArray *)[buffer objectForKey:bufferkey]sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    }

    NSDictionary *result = [NSDictionary dictionaryWithDictionary:buffer];



    return result;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [keys count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSString *key = [_sortedKeys objectAtIndex:section];
    NSArray *wordsForSection = [self.alphabetizedWords objectForKey:key];
    return  [wordsForSection count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSString *key = [_sortedKeys objectAtIndex:[indexPath section]];
    NSArray *wordsForSection = [self.alphabetizedWords objectForKey:key];
    NSString *word = [[wordsForSection objectAtIndex:[indexPath row]]objectForKey:@"Word"];


    [cell.textLabel setText:word];

    return cell;
}

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

    NSString *key = [_sortedKeys objectAtIndex:section];
    return key;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [self performSegueWithIdentifier:@"showDetail" sender:cell];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        detailViewController *destViewController = segue.destinationViewController;

        NSString *key = [_sortedKeys objectAtIndex:[indexPath section]];
        NSArray *wordsForSection = [self.alphabetizedWords objectForKey:key];
        NSString *word = [[wordsForSection objectAtIndex:[indexPath row]]objectForKey:@"Word"];
        destViewController.word = word;
        destViewController.definition = [[wordsForSection objectAtIndex:[indexPath row] ]objectForKey:@"Definition"];

    }
}




- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    NSString *path = [[NSBundle mainBundle]pathForResource:@"words" ofType:@"plist"];

    NSArray *wordsDictionary = [NSArray arrayWithContentsOfFile:path];
    self.words = wordsDictionary;

    self.alphabetizedWords = [self alphabetizedWords:self.words];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];

    self.keys = [self.alphabetizedWords allKeys];

    self.sortedKeys = [keys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

2 个答案:

答案 0 :(得分:1)

您正在创建一个词典数组。字典不支持比较方法。因此,您无法使用基于选择器的排序。您需要使用不同的排序方法,如sortUsingComparator或谓词。 (有许多不同的方法可以对数组进行排序。)

答案 1 :(得分:0)

如果您只需要反转数组元素的位置,可以使用以下

NSArray *wordArray = [[NSArray alloc] initWithObjects:@"Brian",@"Job",@"Bob",@"Ben",@"Robert", nil];
wordArray = [[wordArray reverseObjectEnumerator] allObjects];

您的输出将是:Robert,Ben,Bob,Job,Brian。