objective-c – 无法在NSManagedObject类上调用指定的初始值设定
另一个新手问题,就在我以为我开始变得非常好的时候
关于ios编程的小手柄.啊!我正在跟随他的导师 appcodeblog.com我正在构建一个简单的标签栏应用程序 核心数据,用于输入,显示和搜索度假目的地.我工作过 通过教程并有一个工作的应用程序,但我注意到我选择了 “显示目的地”选项卡我收到以下错误.该应用程序似乎继续 工作,但错误记录到控制台.我正在尝试调试 问题并准确理解发生了什么,但我只是不完全 明白什么是错的.我“想”我的问题与我有关 ShowDestinations.xib文件,我错误地将我的对象连接到其中 xib.任何帮助深表感谢.在此先感谢您的帮助和 时间. 这是错误,“CoreDataTabBarTutorial [1262:207]无法调用指定的 我不确定要提供什么代码,所以我开始显示我的标题 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 解决方法
问题似乎在于
Destination *destination = [[Destination alloc] init]; destination = (Destination *)[destinationsArray objectAtIndex:indexPath.row]; [destination release]; 第一行是不必要的:在Objective-C中,Destination *是指向对象的指针,而不是真实对象.您想要的Destination对象可能已经在数组中.因此,您不必在[[Destination alloc] init]行中创建一个指向的对象,该行在下一行中立即消失.发生了什么事 > [[Destination alloc] init]创建一个对象a,destination指向a. a由您保留. 所以,相反,只是做 Destination *destination = (Destination *)[destinationsArray objectAtIndex:indexPath.row]; 没有发布部分. 作为建议:在构建项目时始终运行Analyze(可在Build菜单下面找到).分析仪旨在捕捉常见类型的错误,包括您的错误.纠正您的代码,以便所有分析仪警告消失;您应始终将分析仪警告视为您的错误. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |