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

时间:2011-07-19 18:21:16

标签: objective-c xcode core-data

另一个新手问题,就在我以为我开始变得非常好的时候 关于ios编程的小手柄。啊!我正在跟随他的导师 appcodeblog.com我正在构建一个简单的标签栏应用程序 核心数据,用于输入,显示和搜索度假目的地。我工作过 通过教程并有一个工作的应用程序,但我注意到我选择了 “显示目的地”选项卡我收到以下错误。该应用程序似乎继续 工作,但错误记录到控制台。我正在尝试调试 问题并准确理解发生了什么,但我只是不完全 明白什么是错的。我“想”我的问题与我有关 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

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

1 个答案:

答案 0 :(得分:6)

问题似乎在于

Destination *destination = [[Destination alloc] init];
destination = (Destination *)[destinationsArray objectAtIndex:indexPath.row];
[destination release];

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

  1. [[Destination alloc] init]创建一个对象adestination指向a。 {/ 1}}由您保留。
  2. a为您提供了另一个对象(Destination *)[destinationsArray objectAtIndex:indexPath.row],您不会保留该对象。 b现在指向destination。没人再持b
  3. a被发送到release指向的对象,即destination。这违反了保留释放规则;你应该发布b,而不是a
  4. 所以,只需要做

    b

    没有Destination *destination = (Destination *)[destinationsArray objectAtIndex:indexPath.row]; 部分。

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