无法在NSManagedObject类上调用指定的初始化程序

5lb*_*ass 4 xcode core-data objective-c

另一个新手问题,就在我认为我开始在ios编程上获得一个非常小的处理时.啊! 我正在关注appcodeblog.com上的一个tutoria,我正在构建一个简单的标签栏应用程序,利用核心数据输入,显示和搜索度假目的地.我已经完成了教程,并有一个有效的应用程序,但我注意到当我选择"显示目的地"选项卡时,我收到以下错误.该应用程序似乎继续工作,但错误记录到控制台.我正在尝试调试问题并准确理解发生了什么,但我不太明白什么是错的.我"想"我的ShowDestinations.xib文件存在问题,我错误地将我的对象连接到xib中.任何帮助深表感谢.在此先感谢您的帮助和时间.

这是错误,"CoreDataTabBarTutorial [1262:207]无法在NSManagedObject类'Destination'上调用指定的初始值设定项.

我不确定要提供什么代码所以我开始通过显示我的头文件和实现文件ShowDistinationsViewController.h和ShowDestinationsViewController.m

ShowDistinationsViewController.h

#import <UIKit/UIKit.h>


@interface SearchDestinationsViewController : UIViewController {

    UISearchBar *destinationSearchBar;
    UITableView *searchTableView;

    NSFetchedResultsController *fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;

    NSArray *fetchedObjects;

 }

@property (nonatomic, retain) IBOutlet UISearchBar *destinationSearchBar;
@property (nonatomic, retain) IBOutlet UITableView *searchTableView;

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

@end
Run Code Online (Sandbox Code Playgroud)

ShowDestinationsViewController.m

#import "ShowDestinationsViewController.h"
#import "Destination.h"

@implementation ShowDestinationsViewController

@synthesize destinationsTableView;
@synthesize destinationsArray;
@synthesize fetchedResultsController;
@synthesize managedObjectContext;

// Not sure where the following code came from so I commented it out!!! It didn't seem to break anything when I commented it out
//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        // Custom initialization
//    }
//    return self;
//}

- (void)dealloc
{
    [destinationsArray release];
    [destinationsTableView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a      nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark -
#pragma Data Fetch from Core Data

- (void) viewWillAppear:(BOOL)animated
{

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Destination" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];
    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil)
    {
        // Handle the error.
        NSLog(@"mutableFetchResults == nil");
    }
    [self setDestinationsArray:mutableFetchResults];
    [request release];
    [destinationsTableView reloadData];
} 


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [destinationsArray count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    Destination *destination = [[Destination alloc] init];
    destination = (Destination *)[destinationsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = destination.name;
    [destination release];

    return cell;
}

#pragma mark -
#pragma mark Table view delegate

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

}

@end
Run Code Online (Sandbox Code Playgroud)

Yuj*_*uji 6

问题似乎在于

Destination *destination = [[Destination alloc] init];
destination = (Destination *)[destinationsArray objectAtIndex:indexPath.row];
[destination release];
Run Code Online (Sandbox Code Playgroud)

第一行是不必要的:在Objective-C中,Destination*是指向对象的指针,而不是真实对象.Destination您想要的对象可能已经在数组中.因此,您不必在行中创建一个指向[[Destination alloc] init]下一行的对象.发生了什么事

  1. [[Destination alloc] init]创建一个对象a,destination指向a.a由你保留.
  2. (Destination *)[destinationsArray objectAtIndex:indexPath.row]给你另一个对象b,你不会保留它.destination现在指向b.没有人再坚持了a.
  3. release被发送到指向的对象destination,即b.这违反了保留释放规则; 你应该释放a,而不是b!

所以,相反,只是做

Destination *destination = (Destination *)[destinationsArray objectAtIndex:indexPath.row];
Run Code Online (Sandbox Code Playgroud)

没有release部分.

作为建议:在构建项目时始终运行Analyze(在Build菜单下方可用).分析仪旨在捕捉常见类型的错误,包括您的错误.纠正您的代码,以便所有分析仪警告消失; 您应始终将分析仪警告视为您的错误.