关于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解决方法 问题似乎在于
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 *)[destinationsArray objectAtIndex:indexPath.row]为您提供另一个对象b,您不会保留该对象.目的地现在指向b.没有人再坚持了.
> release被发送到目的地指向的对象,即b.这违反了保留释放规则;你应该释放一个,而不是b!
所以,相反,只是做
Destination *destination = (Destination *)[destinationsArray objectAtIndex:indexPath.row];
没有发布部分.
作为建议:在构建项目时始终运行Analyze(可在Build菜单下面找到).分析仪旨在捕捉常见类型的错误,包括您的错误.纠正您的代码,以便所有分析仪警告消失;您应始终将分析仪警告视为您的错误.
总结以上是内存溢出为你收集整理的objective-c – 无法在NSManagedObject类上调用指定的初始值设定项全部内容,希望文章能够帮你解决objective-c – 无法在NSManagedObject类上调用指定的初始值设定项所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)