核心数据未解决错误(null),(null)

时间:2012-04-10 22:42:18

标签: core-data ios5

我昨天有一个这个应用程序的版本。我重新开始,因为我想要练习并添加一些功能。但是今天我无法通过为实体添加新对象来完成最基本的任务。当我尝试保存时,我不断收到同样的错误:

  

未解决的错误(null),(null)

大多数早期代码都是从工作应用中复制并粘贴的。当这不起作用时,我创建了一个新项目并重新输入了所有代码。没有骰子。所以我删除了所有派生数据,删除了所有早期版本的应用程序,重置了模拟器,关闭了Xcode,重新启动了我的机器,创建了一个新项目并重新输入了这四个类。但我仍然得到同样的臭味。我的模型中没有强制属性。我在这里靠墙打我的头。有任何想法吗?这是我正在使用的类的代码:

//  PublisherTableViewController.h

#import <UIKit/UIKit.h>
#import "Publisher.h"


@interface PublisherTableViewController : UITableViewController <NSFetchedResultsControllerDelegate, PublisherAddDelegate>{
    @private

    NSFetchedResultsController *fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;



}

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;

// PublisherTableViewController.m

#import "AddPublisherTableViewController.h"
#import "PublisherTableViewController.h"
#import "AppDelegate.h"


@interface PublisherTableViewController ()

@end

@implementation PublisherTableViewController
@synthesize fetchedResultsController, managedObjectContext;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"AddPublisherSegue"]) {
        AddPublisherTableViewController *addPublisherController = (AddPublisherTableViewController *) [[segue destinationViewController] topViewController];
        addPublisherController.delegate = self;

        Publisher *newPublisher = [NSEntityDescription insertNewObjectForEntityForName:@"Publisher" inManagedObjectContext:self.managedObjectContext];
        addPublisherController.publisher = newPublisher;
    }
}

-(void)publisherAddViewController:(AddPublisherTableViewController *)publisherAddViewController didAddPublisher:(Publisher *)publisher
{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)viewDidLoad
{

    if (managedObjectContext == nil) 
    { 
        managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
        //NSLog(@"After managedObjectContext: %@",  managedObjectContext);
    }

    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error ]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }

    [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.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return [[self.fetchedResultsController sections]count];


}

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

    // Return the number of rows in the section.

    id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];

    return [sectionInfo numberOfObjects];
}

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

    // Configure the cell...

    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    Publisher *publisherForCell = (Publisher *)[fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",publisherForCell.firstName, publisherForCell.lastName];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

- (NSFetchedResultsController *)fetchedResultsController {
    // Set up the fetched results controller if needed.


    if (fetchedResultsController == nil) {

        // Create the fetch request for the entity.
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Publisher" inManagedObjectContext:managedObjectContext];
        [fetchRequest setEntity:entity];

        // Edit the sort key as appropriate.
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

        [fetchRequest setSortDescriptors:sortDescriptors];

        // Edit the section name key path and cache name if appropriate.
        // nil for section name key path means "no sections".
        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
        aFetchedResultsController.delegate = self;
        self.fetchedResultsController = aFetchedResultsController;

        //        [aFetchedResultsController release];
        //        [fetchRequest release];
        //        [sortDescriptor release];
        //        [sortDescriptors release];
    }

    return fetchedResultsController;
}    


/**
 Delegate methods of NSFetchedResultsController to respond to additions, removals and so on.
 */

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    UITableView *tableView = self.tableView;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:(UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


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

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
    [self.tableView endUpdates];
}


@end

// AddPublisherTableViewController.h

#import <UIKit/UIKit.h>
#import "Publisher.h"
@protocol PublisherAddDelegate;

@interface AddPublisherTableViewController : UITableViewController{
    Publisher *publisher;
    UITextField *firstName;
    UITextField *lastName;

// AddPublisherTableViewController.m

#import "AddPublisherTableViewController.h"

@interface AddPublisherTableViewController ()

@end

@implementation AddPublisherTableViewController

@synthesize delegate, firstName, lastName, publisher;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (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.rightBarButtonItem = self.editButtonItem;
}


-(IBAction)savePublisher
{
    publisher.firstName = firstName.text;
    publisher.lastName = lastName.text;

    NSError *error = nil;

    if (![publisher.managedObjectContext save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }

    [self.delegate publisherAddViewController:self didAddPublisher:publisher];

}

-(IBAction)cancel
{
    if (publisher.firstName == NULL) {
        [publisher.managedObjectContext deleteObject:publisher];

        NSError *error = nil;

        if (![publisher.managedObjectContext save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }
        [self.delegate publisherAddViewController:self didAddPublisher:nil];
    }else {
        [self.delegate publisherAddViewController:self didAddPublisher:publisher];
    }
}    

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
    id<PublisherAddDelegate> delegate;

}

@property (nonatomic, strong) Publisher *publisher;
@property (nonatomic, strong) IBOutlet UITextField *firstName;
@property (nonatomic, strong) IBOutlet UITextField *lastName;
@property (nonatomic, strong) id<PublisherAddDelegate> delegate;

-(IBAction)savePublisher;
-(IBAction)cancel;

@end

@protocol PublisherAddDelegate <NSObject>

-(void)publisherAddViewController:(AddPublisherTableViewController *)publisherAddViewController didAddPublisher:(Publisher *)publisher;

@end

2 个答案:

答案 0 :(得分:1)

属性fetchedResultsController和类变量不能具有相同的名称。 你正在疯狂地驱动编译器。 试试这个:

//  PublisherTableViewController.h

#import <UIKit/UIKit.h>
#import "Publisher.h"


@interface PublisherTableViewController : UITableViewController <NSFetchedResultsControllerDelegate, PublisherAddDelegate>{
    @private

    NSFetchedResultsController *mFetchedResultsController;
    NSManagedObjectContext *mManagedObjectContext;
}

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;

// PublisherTableViewController.m

#import "AddPublisherTableViewController.h"
#import "PublisherTableViewController.h"
#import "AppDelegate.h"


@interface PublisherTableViewController ()

@end

@implementation PublisherTableViewController
@synthesize fetchedResultsController = mFetchedResultsController
@synthesize managedObjectContext = mManagedObjectContext;

...

fetchedResultsController之前可以调用viewDidLoad,因此当您尝试初始化提取的结果控制器时,managedObjectContext仍可能是null

答案 1 :(得分:0)

没关系。显然xcode讨厌我。我添加了几个断点和nslog,因此请查看我的文本字段和发布者属性的值。我第一次构建并运行项目时,文本字段返回时带有正确的值,但发布者属性仍然为null。虽然持续不断,我只是重建并再次运行项目而不改变任何东西,现在它只是花花公子。谢谢Xcode!