Core data 核心数据未解析错误(null),(null)
我昨天安装并运行了这个应用程序的一个版本。我重新开始是因为我想练习并添加一些功能。但今天,我无法完成为实体添加新对象的最基本任务。在尝试保存时,我不断遇到相同的错误: 未解决的错误(null),(null) 大部分早期代码都是从工作应用程序中复制和粘贴的。当这不起作用时,我创建了一个新项目并重新键入了所有代码。没有骰子。因此,我删除了所有派生数据,删除了应用程序的所有早期版本,重置了模拟器,关闭了Xcode,重新启动了我的机器,创建了一个新项目,并重新键入了这四个类。但我还是犯了同样的错误。我的模型中没有强制属性。我在这里把头撞在墙上。有什么想法吗?以下是我正在使用的类的代码:Core data 核心数据未解析错误(null),(null),core-data,ios5,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.h
#进口
#导入“Publisher.h”
@接口PublisherTableViewController:UITableViewController{
@私人的
NSFetchedResultsController*fetchedResultsController;
NSManagedObjectContext*managedObjectContext;
}
@属性(强,非原子)NSFetchedResultsController*fetchedResultsController;
@属性(强,非原子)NSManagedObjectContext*managedObjectContext;
-(void)configureCell:(UITableViewCell*)单元格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
#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
#import "AddPublisherTableViewController.h"
#import "PublisherTableViewController.h"
#import "AppDelegate.h"
@interface PublisherTableViewController ()
@end
@implementation PublisherTableViewController
@synthesize fetchedResultsController = mFetchedResultsController
@synthesize managedObjectContext = mManagedObjectContext;
...
#导入“AddPublisherTableViewController.h”
#导入“PublisherTableViewController.h”
#导入“AppDelegate.h”
@接口PublisherTableViewController()
@结束
@实现PublisherTableViewController
@综合fetchedResultsController、managedObjectContext;
-(id)initWithStyle:(UITableViewStyle)样式
{
self=[super initWithStyle:style];
如果(自我){
//自定义初始化
}
回归自我;
}
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方
{
if([[segue identifier]IsequalString:@“addPublishersgue”]){
AddPublisherTableViewController*addPublisherController=(AddPublisherTableViewController*)[[segue destinationViewController]topViewController];
addPublisherController.delegate=self;
Publisher*newPublisher=[NSEntityDescription insertNewObjectForEntityForName:@“Publisher”在managedObjectContext:self.managedObjectContext]中;
addPublisherController.publisher=newPublisher;
}
}
-(void)PublisherADVIEWController:(添加PublisherTableViewController*)PublisherADVIEWController didAddPublisher:(发布者*)发布者
{
[自我解散Modalviewcontrolleranimated:是];
}
-(无效)viewDidLoad
{
if(managedObjectContext==nil)
{
managedObjectContext=[(AppDelegate*)[[UIApplication sharedApplication]委托]managedObjectContext];
//NSLog(@“在managedObjectContext之后:%@”,managedObjectContext);
}
n错误*错误=nil;
如果(![[self-fetchedResultsController]性能检测:&错误]){
NSLog(@“未解决的错误%@,%@”,错误,[error userInfo]);
}
[超级视图下载];
//取消对下一行的注释以保留演示文稿之间的选择。
//self.clearselectiononviewwillappear=否;
//取消对以下行的注释,以在此视图控制器的导航栏中显示编辑按钮。
//self.navigationItem.rightBarButtonItem=self.editButtonItem;
}
-(无效)视图卸载
{
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
//返回节数。
返回[[self.fetchedResultsController节]计数];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
//返回节中的行数。
id sectionInfo=[[self.fetchedResultsController节]objectAtIndex:section];
返回[sectionInfo numberOfObjects];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“PublisherCell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//配置单元格。。。
[self-configureCell:cell-atIndexPath:indexPath];
返回单元;
}
-(void)configureCell:(UITableViewCell*)单元格atIndexPath:(NSIndexPath*)indexPath
{
Publisher*Publisher-forcell=(Publisher*)[fetchedResultsController对象索引路径:indexPath];
cell.textLabel.text=[NSString stringWithFormat:@“%@%@”,publisherForCell.firstName,publisherForCell.lastName];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
/*
//替代以支持表视图的条件编辑。
-(BOOL)tableView:(UITableView*)tableView caneditrowatinexpath:(nsindepath*)indepath
{
//如果不希望指定的项可编辑,则返回“否”。
返回YES;
}
*/
/*
//替代以支持编辑表格视图。
-(void)tableView:(UITableView*)tableView提交的编辑样式:(UITableViewCellEditingStyle)行的编辑样式索引路径:(NSIndexPath*)索引路径
{
如果(editingStyle==UITableViewCellEditingStyleDelete){
//从数据源中删除该行
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]和RowAnimation:UITableViewRowAnimationFade];
}
else if(editingStyle==UITableViewCellEditingStyleInsert){
//创建相应类的新实例,将其插入数组,并向表视图添加新行
}
}
*/
/*
//替代以支持重新排列表视图。
-(void)tableView:(UITableView*)tableView移动rowatinexpath:(nsindepath*)从indepath到indepath:(nsindepath*)到indepath
{
}
*/
/*
//超越支持
// 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;
#import "AddPublisherTableViewController.h"
#import "PublisherTableViewController.h"
#import "AppDelegate.h"
@interface PublisherTableViewController ()
@end
@implementation PublisherTableViewController
@synthesize fetchedResultsController = mFetchedResultsController
@synthesize managedObjectContext = mManagedObjectContext;
...