Iphone entityForName&;扩展教程材料时的ManagedObjectContext
下午好, 我试图在(locations)coredata教程代码中向持久存储添加第二个数据实体,然后在新视图中访问它。我想我已经遵循了教程,并检查了我是否正在进行一个干净的构建等,但看不到要更改什么以防止崩溃 恐怕我对这件事束手无策,似乎找不到我错过的那一步。我已经在下面粘贴了头文件和代码文件,如果需要共享更多代码,请告诉我。事故似乎发生在线路上:Iphone entityForName&;扩展教程材料时的ManagedObjectContext,iphone,ios,core-data,Iphone,Ios,Core Data,下午好, 我试图在(locations)coredata教程代码中向持久存储添加第二个数据实体,然后在新视图中访问它。我想我已经遵循了教程,并检查了我是否正在进行一个干净的构建等,但看不到要更改什么以防止崩溃 恐怕我对这件事束手无策,似乎找不到我错过的那一步。我已经在下面粘贴了头文件和代码文件,如果需要共享更多代码,请告诉我。事故似乎发生在线路上: NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album"
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:[self managedObjectContext]];
目前代码中还有一行引用galleryviewcontroller,它位于主应用程序委托中:
galleryViewController.managedObjectContext = [self managedObjectContext];
GalleryViewController.h
#import <UIKit/UIKit.h>
@interface GalleryViewController : UIViewController {
NSManagedObjectContext *managedObjectContext;
int rowNumber;
IBOutlet UILabel *lblMessage;
UIBarButtonItem *addButton;
NSMutableArray *imagesArray;
}
@property (readwrite) int rowNumber;
@property (nonatomic,retain) UILabel *lblMessage;
@property (nonatomic,retain) NSMutableArray *imagesArray;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) UIBarButtonItem *addButton;
-(void)updateRowNumber:(int)theIndex;
-(void)addImage;
@end
#导入
@界面GalleryViewController:UIViewController{
NSManagedObjectContext*managedObjectContext;
整数行数;
IBUILabel*LBL消息;
UIBarButtonItem*添加按钮;
NSMutableArray*图像阵列;
}
@属性(读写)int rowNumber;
@属性(非原子,保留)UILabel*LBL消息;
@属性(非原子,保留)NSMutableArray*imagesArray;
@属性(非原子,保留)NSManagedObjectContext*managedObjectContext;
@属性(非原子,保留)UIBarButtonItem*addButton;
-(void)updateRownNumber:(int)索引;
-(无效)添加图像;
@结束
GalleryViewController.m
#import "RootViewController.h"
#import "LocationsAppDelegate.h"
#import "Album.h"
#import "GalleryViewController.h"
#import "Image.h"
@implementation GalleryViewController
@synthesize lblMessage,rowNumber,addButton,managedObjectContext;
@synthesize imagesArray;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
-(void)updateRowNumber:(int)theIndex{
rowNumber=theIndex;
LocationsAppDelegate *mainDelegate =(LocationsAppDelegate *)[[UIApplication sharedApplication] delegate];
Album *anAlbum = [mainDelegate.albumsArray objectAtIndex:rowNumber];
lblMessage.text = anAlbum.uniqueAlbumIdentifier;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addImage)];
addButton.enabled = YES;
self.navigationItem.rightBarButtonItem = addButton;
/* Found this in another answer, adding it to the code didn't help.
if (managedObjectContext == nil) {
managedObjectContext = [[[UIApplication sharedApplication] delegate] managedObjectContext];
}
*/
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:[self managedObjectContext]];
[request setEntity:entity];
// Order the albums by creation date, most recent first.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"imagePath" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];
// Execute the fetch -- create a mutable copy of the result.
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
[self setImagesArray:mutableFetchResults];
int a = 5;
int b = 10;
for( int i=0; i<[imagesArray count]; i++ )
{
if( a == 325 )
{
a = 5;
b += 70;
}
UIImageView *any = [[UIImageView alloc] initWithFrame:CGRectMake(a,b,70,60)];
any.image = [imagesArray objectAtIndex:i];
any.tag = i;
[self.view addSubview:any];
[any release];
a += 80;
}
}
-(void)addImage{
NSString *msg = [NSString stringWithFormat:@"%i",rowNumber];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add image to" message:msg
delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alert show];
[alert release];
}
- (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.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[lblMessage release];
[managedObjectContext release];
[super dealloc];
}
@end
#导入“RootViewController.h”
#导入“LocationsAppDelegate.h”
#导入“Album.h”
#导入“GalleryViewController.h”
#导入“Image.h”
@GalleryViewController的实现
@综合LBL消息、行号、addButton、managedObjectContext;
@合成图像射线;
/*
//指定的初始值设定项。如果以编程方式创建控制器并希望执行不适用于viewDidLoad的自定义,请重写。
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
if((self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])){
//自定义初始化
}
回归自我;
}
*/
-(void)updateRownNumber:(int)索引{
行数=索引;
LocationsAppDelegate*mainDelegate=(LocationsAppDelegate*)[[UIApplication sharedApplication]委托];
Album*anAlbum=[mainDelegate.albumsArray对象索引:行编号];
lblMessage.text=anAlbum.uniqueAlbumIdentifier;
}
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad{
[超级视图下载];
addButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemAddTarget:self action:@selector(addImage)];
addButton.enabled=是;
self.navigationItem.rightBarButtonItem=addButton;
/*在另一个答案中发现了这一点,将其添加到代码中没有帮助。
if(managedObjectContext==nil){
managedObjectContext=[[UIApplication sharedApplication]委托]managedObjectContext];
}
*/
NSFetchRequest*request=[[NSFetchRequest alloc]init];
NSEntityDescription*entity=[NSEntityDescription entityForName:@“Album”inManagedObjectContext:[self-managedObjectContext]];
[请求集合实体:实体];
//按创建日期订购相册,最新日期优先。
NSSortDescriptor*sortDescriptor=[[NSSortDescriptor alloc]initWithKey:@“imagePath”升序:否];
NSArray*sortDescriptors=[[NSArray alloc]initWithObjects:sortDescriptor,nil];
[请求集合描述符:排序描述符];
[sortDescriptor发布];
[sortDescriptors发布];
//执行fetch——创建结果的可变副本。
n错误*错误=nil;
NSMutableArray*mutableFetchResults=[[managedObjectContext executeFetchRequest:请求错误:&错误]mutableCopy];
if(mutableFetchResults==nil){
//处理错误。
}
[self-setImagesArray:mutableFetchResults];
INTA=5;
int b=10;
对于(int i=0;i来说,发布您得到的错误会有所帮助
您是否尝试过以下方法:
- 项目的干净构建
- 重置模拟器以删除旧的sqlite数据文件
如果这两个都不能解决问题,那么将您得到的错误作为问题的更新发布
更新
我要查找的信息将在控制台中打印出来,而调试器是一个非常有用的工具,我建议您学习它,在这种情况下,您的问题可以通过查看应用程序的控制台输出来解决。感谢您的建议,我没有找到重置模拟器选项,并认为删除应用程序将删除所有相关数据。重置似乎完成了一项更彻底的工作,代码现在运行无误。您好,Martin。我还不完全熟悉调试器,发布对我来说最有用的信息是什么,我在哪里可以找到这些信息?Martin