线程 1:信号 SIGABRT

Thread 1 : signal SIGABRT

我是 Objective.I 的新手,我正在尝试制作一个简单的应用程序,在添加更多视图后我收到此错误 "Thread 1 Signal SIGABRT" 并且该应用程序无法在 iOS 模拟器中打开。错误指向这行代码:

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

我之前尝试过搜索,但我不明白其他答案在说什么。

NamesTableViewController.h 是:

#import <UIKit/UIKit.h>


@interface NamesTableViewController : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>


@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;

@end

NamesTableViewController.m 是:

#import "NamesTableViewController.h"

@interface NamesTableViewController ()
@property (nonatomic, copy) NSDictionary *propertyList;
@property (nonatomic, copy) NSArray *letters;
@property (nonatomic, copy)NSMutableArray *filteredNames;
@property (nonatomic, strong)UISearchController *searchController;


@end

    @implementation NamesTableViewController

    @synthesize propertyList,  letters, filteredNames, searchController;

    - (void)viewDidLoad {
        [super viewDidLoad];

        UITableView *tableView = (id)[self.view viewWithTag:1];

        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

        filteredNames = [[NSMutableArray alloc]init];
        searchController = [[UISearchController alloc]init];


        self.searchController.searchResultsUpdater = self;

        NSString *path = [[NSBundle mainBundle] pathForResource:@"names" ofType:@"plist"];
        self.propertyList = [NSDictionary dictionaryWithContentsOfFile:path];
        self.letters = [[self.propertyList allKeys] sortedArrayUsingSelector:@selector(compare:)];
    }

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        if (tableView.tag == 1){

            return self.letters.count;

        }else {
            return 1;
        }

        }

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

        if (tableView.tag == 1) {

            NSString *letter = self.letters[section];
            NSArray *keyValues = [self.propertyList[letter] allKeys];
            return keyValues.count;
        } else {


            return [filteredNames count];
        }
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        // Configure the cell...

        if (tableView.tag == 1){

            NSString *letter = self.letters[indexPath.section];;
            NSArray *keyValues = [[self.propertyList[letter] allKeys] sortedArrayUsingSelector:@selector(compare:)];
            cell.textLabel.text = keyValues[indexPath.row];
        } else{
            cell.textLabel.text = filteredNames[indexPath.row];
        }
        return cell;
    }

    -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        return self.letters;
    }

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

        if (tableView.tag == 1) {
            return letters [section];
        } else {
            return nil;
        }
    }

    #pragma mark Search Display Delegate Methods

    -(void)searchDisplayController:(UISearchController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    }



    -(BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString

    {

        [filteredNames removeAllObjects];
        if (searchString.length > 0) {
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.searchBar.text];

            for (NSString *letter in letters) {
                NSArray *matches = [[self.propertyList[letter] allKeys]filteredArrayUsingPredicate:predicate];

                [filteredNames addObjectsFromArray:matches];

            }

        }

        return YES;
    }

    @end

如果您需要更多信息,请通过答案告诉我,我将编辑我的问题,然后您将编辑您的答案

[keys allKeys] returns 单级数组。所以只能有一段。

编辑:

为了更好的理解和可读性,我将 names 替换为 letters,将 keys 替换为 propertyList

•.h

方法 numberOfRowsInSectioncellForRowAtIndexPath 检索节字母,然后从 属性 列表中检索适当的键。

字典作为数据源不是最好的选择,因为有很多重复的任务要执行,例如排序。最好在 viewDidLoad

中创建一个嵌套数组作为数据源
@interface NamesTableViewController ()
@property (nonatomic, copy) NSDictionary *propertyList;
@property (nonatomic, copy) NSArray *letters;

@end

•.m

@implementation NamesTableViewController

@synthesize propertyList, letters;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"names" ofType:@"plist"];
    self.propertyList = [NSDictionary dictionaryWithContentsOfFile:path];
    self.letters = [[self.propertyList allKeys] sortedArrayUsingSelector:@selector(compare:)];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.letters.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *letter = self.letters[section];
    NSArray *keyValues = [self.propertyList[letter] allKeys];
    return keyValues.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    NSString *letter = self.letters[indexPath.section];
    NSArray *keyValues = [[self.propertyList[letter] allKeys] sortedArrayUsingSelector:@selector(compare:)];
    cell.textLabel.text = keyValues[indexPath.row];
    return cell;
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.letters;
}

@end