Apress More IOS 6开发第3章中的错误

Apress More IOS 6开发第3章中的错误,ios,xcode,Ios,Xcode,是否有人尝试过“更多iOS 6开发”中的SuperDB应用程序?当我点击“+”按钮时,我得到了这个错误。我完全按照书中的内容去做,但我不知道哪里错了。我正在xcode 5中运行应用程序。请告知 SuperDB[1676:a0b]CoreData:错误:严重的应用程序错误。在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托捕获到异常。尝试将行0插入节0,但在使用userInfo(null)进行更新后,节0中只有0行 #导

是否有人尝试过“更多iOS 6开发”中的SuperDB应用程序?当我点击“+”按钮时,我得到了这个错误。我完全按照书中的内容去做,但我不知道哪里错了。我正在xcode 5中运行应用程序。请告知

SuperDB[1676:a0b]CoreData:错误:严重的应用程序错误。在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托捕获到异常。尝试将行0插入节0,但在使用userInfo(null)进行更新后,节0中只有0行

#导入“HeroListController.h”
#导入“AppDelegate.h”
@接口控制器()
@属性(非原子、强、只读)NSFetchedResultsController*fetchedResultsController;
@结束
@ListController的实现
@综合fetchedResultsController=\u fetchedResultsController;
-(无效)viewDidLoad
{
[超级视图下载];
//取消对下一行的注释以保留演示文稿之间的选择。
//self.clearselectiononviewwillappear=否;
//取消对以下行的注释,以在此视图控制器的导航栏中显示编辑按钮。
self.navigationItem.leftBarButtonItem=self.editButtonItem;
//选择选项卡栏按钮
NSUserDefaults*默认值=[NSUserDefaults standardUserDefaults];
NSInteger selectedTab=[默认整数工作:kSelectedTabDefaultsKey];
uitabaritem*item=[self.herotabar.items对象索引:selectedTab];
[self.herotabar setSelectedItem:item];
//获取任何现有实体
n错误*错误=nil;
如果(![[self-fetchedResultsController]性能检测:&错误]){
UIAlertView*警报=
[[UIAlertView alloc]
initWithTitle:NSLocalizedString(@“加载数据时出错”,
@“加载数据时出错”)
消息:[NSString stringWithFormat:NSLocalizedString(@“错误为:%@,正在退出。”,
@“错误为:%@,正在退出。”),
[错误本地化描述]]
代表:赛尔夫
取消按钮:NSLocalizedString(@“Aw,螺母”,“Aw,螺母”)
其他按钮:无];
[警报显示];
}
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回[[self.fetchedResultsController节]计数];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[[[self.fetchedResultsController节]对象索引:节]numberOfObjects];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“HeroListCell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//配置单元格。。。
NSManagedObject*aHero=[self.fetchedResultsController对象索引路径:indexPath];
NSInteger选项卡=[self.herotabar.items indexOfObject:self.herotabar.selectedItem];
开关(选项卡){
案例kByName:
cell.textlab.text=[aHero valueForKey:@“name”];
cell.detailTextLabel.text=[aHero valueForKey:@“机密性”];
打破
案例kBySecretIdentity:
cell.textlab.text=[aHero valueForKey:@“机密性”];
cell.detailTextLabel.text=[aHero valueForKey:@“name”];
打破
}
返回单元;
}
//替代以支持编辑表格视图。
-(void)tableView:(UITableView*)tableView
提交的编辑样式:(UITableViewCellEditingStyle)编辑样式
ForRowatineXpath:(NSIndexPath*)indexPath
{
NSManagedObjectContext*managedObjectContext=[self.fetchedResultsController
managedObjectContext];
如果(editingStyle==UITableViewCellEditingStyleDelete){
//从数据源中删除该行
[managedObjectContext deleteObject:[self.fetchedResultsController]
objectAtIndexPath:indexath]];
n错误*错误;
如果(![managedObjectContext保存:&错误]){
UIAlertView*警报=[[UIAlertView alloc]
initWithTitle:NSLocalizedString(@“错误保存实体”,
@“保存实体时出错”)
信息:
[NSString stringWithFormat:NSLocalizedString(@“错误为:%@,正在退出。”,
@“错误为:%@,正在退出。”),[Error localizedDescription]]
代表:赛尔夫
取消按钮:NSLocalizedString(@“Aw,螺母”,“Aw,螺母”)
其他按钮:无];
[警报显示];
}
}
}
#pragma-mark-uitabardelegate方法
-(void)选项卡栏:(UITabBar*)选项卡栏didSelectItem:(UITabBarItem*)项
{
NSUserDefaults*默认值=[NSUserDefaults standardUserDefaults];
NSUInteger tabIndex=[tabBar.items indexOfObject:item];
[默认值setInteger:tabIndex-forKey:kSelectedTabDefaultsKey];
[NSFetchedResultsController deleteCacheWithName:@“Hero”];
_fetchedResultsController.delegate=nil;
_fetchedResultsController=nil;
n错误*错误;
如果(![self.fetchedResultsController性能检测:&错误]){
NSLog(@“执行获取时出错:%@,[Error localizedDescription]);
}
[self.heroTableView重载数据];
}
#pragma标记-FetchedResultsController属性
-(NSFetchedResultsController*)fetchedResultsController
{
如果(_fetchedResultsController!=nil){
返回_fetchedResultsController;
}
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init
#import "HeroListController.h"
#import "AppDelegate.h"

@interface HeroListController ()
@property (nonatomic, strong, readonly) NSFetchedResultsController *fetchedResultsController;
@end

@implementation HeroListController

@synthesize fetchedResultsController=_fetchedResultsController;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    // Select the Tab Bar button
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSInteger selectedTab = [defaults integerForKey:kSelectedTabDefaultsKey];
    UITabBarItem *item = [self.heroTabBar.items objectAtIndex:selectedTab];
    [self.heroTabBar setSelectedItem:item];

    // Fetch any existing entities
    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        UIAlertView *alert =
        [[UIAlertView alloc]
         initWithTitle:NSLocalizedString(@"Error loading data",
                                         @"Error loading data")
         message:[NSString stringWithFormat:NSLocalizedString(@"Error was: %@, quitting.",
                                                              @"Error was: %@, quitting."),
                  [error localizedDescription]]
         delegate:self
         cancelButtonTitle:NSLocalizedString(@"Aw, Nuts", @"Aw, Nuts")
         otherButtonTitles:nil];
        [alert show];
    }
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}

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

    // Configure the cell...
    NSManagedObject *aHero = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSInteger tab = [self.heroTabBar.items indexOfObject:self.heroTabBar.selectedItem];
    switch (tab) {
        case kByName:
            cell.textLabel.text = [aHero valueForKey:@"name"];
            cell.detailTextLabel.text = [aHero valueForKey:@"secretIdentity"];
            break;
        case kBySecretIdentity:
            cell.textLabel.text = [aHero valueForKey:@"secretIdentity"];
            cell.detailTextLabel.text = [aHero valueForKey:@"name"];
            break;
    }
    return cell;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObjectContext *managedObjectContext=[self.fetchedResultsController
                                                  managedObjectContext];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [managedObjectContext deleteObject:[self.fetchedResultsController
                                            objectAtIndexPath:indexPath]];
        NSError *error;
        if (![managedObjectContext save:&error]) {
            UIAlertView *alert= [[UIAlertView alloc]
                                 initWithTitle:NSLocalizedString(@"Error saving entity",
                                                                 @"Error saving entity")
                                 message:
                                 [NSString stringWithFormat:NSLocalizedString(@"Error was: %@, quitting.",
                                                                              @"Error was: %@, quitting."), [error localizedDescription]]
                                 delegate:self
                                 cancelButtonTitle:NSLocalizedString(@"Aw, Nuts", @"Aw, Nuts")
                                 otherButtonTitles:nil];
            [alert show];
        }
    }
}

#pragma mark - UITabBarDelegate Methods
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    NSUInteger tabIndex=[tabBar.items indexOfObject:item];
    [defaults setInteger:tabIndex forKey:kSelectedTabDefaultsKey];

    [NSFetchedResultsController deleteCacheWithName:@"Hero"];
    _fetchedResultsController.delegate = nil;
    _fetchedResultsController = nil;

    NSError *error;
    if (![self.fetchedResultsController performFetch:&error]) {
        NSLog(@"Error performing fetch: %@", [error localizedDescription]);
    }
    [self.heroTableView reloadData];
}

#pragma mark - FetchedResultsController Property
- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Hero"
                                              inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    [fetchRequest setFetchBatchSize:20];

    NSUInteger tabIndex = [self.heroTabBar.items indexOfObject:self.heroTabBar.selectedItem];
    if (tabIndex == NSNotFound) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        tabIndex = [defaults integerForKey:kSelectedTabDefaultsKey];
    }

    NSString *sectionKey = nil;
    switch (tabIndex) {
            // Notice that the kByName and kBySecretIdentity Code are nearly identical.
            // A refactoring opportunity?
        case kByName: {
            NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                                            ascending:YES];
            NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"secretIdentity"
                                                                            ascending:YES];
            NSArray *sortDescriptors =
            [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
            [fetchRequest setSortDescriptors:sortDescriptors];
            sectionKey = @"name";
            break;
        }
        case kBySecretIdentity:{
            NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"secretIdentity"
                                                                            ascending:YES];
            NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                                            ascending:YES];
            NSArray *sortDescriptors =
            [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
            [fetchRequest setSortDescriptors:sortDescriptors];
            sectionKey = @"secretIdentity";
            break;
        }
    }

    _fetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:managedObjectContext sectionNameKeyPath:sectionKey
                                                   cacheName:@"Hero"];
    _fetchedResultsController.delegate = self;
    return _fetchedResultsController;
}

#pragma mark - NSFetchedResultsControllerDelegate Methods
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    [self.heroTableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.heroTableView endUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id < NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex
     forChangeType:(NSFetchedResultsChangeType)type
{
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.heroTableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                              withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [self.heroTableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                              withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controller:(NSFetchedResultsController *)controller
   didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath
     forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.heroTableView insertRowsAtIndexPaths:@[newIndexPath]
                                      withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [self.heroTableView deleteRowsAtIndexPaths:@[indexPath]
                                      withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeUpdate:
        case NSFetchedResultsChangeMove:
            break;
    }
}

#pragma mark - UIAlertViewDelegate Methods
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    exit(-1);
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    self.addButton.enabled = !editing;
    [self.heroTableView setEditing:editing animated:animated];
}

- (IBAction)addHero:(id)sender {
    NSManagedObjectContext *managedObjectContext= [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    [NSEntityDescription insertNewObjectForEntityForName:[entity name]
                                  inManagedObjectContext:managedObjectContext];
    NSError *error = nil;
    if (![managedObjectContext save:&error]) {
        UIAlertView *alert =
        [[UIAlertView alloc]
         initWithTitle:NSLocalizedString(@"Error saving entity",
                                         @"Error saving entity")
                                        message:[NSString stringWithFormat:NSLocalizedString(@"Error was: %@, quitting.", @"Error was: %@, quitting."),
                  [error localizedDescription]]
         delegate:self
         cancelButtonTitle:NSLocalizedString(@"Aw, Nuts", @"Aw, Nuts")
         otherButtonTitles:nil];
        [alert show];
    }
}

@end