Hi there.
I am trying to save the dimensions of my document window through Core Data. I have used the following code to save the width and height:
managedObjectContext = [[[NSDocumentController sharedDocumentController] currentDocument] managedObjectContext];
NSSize windowSize;
windowSize.width = documentWindow.frame.size.width;
windowSize.height = documentWindow.frame.size.height;
SizeInfo *windowInfo = (SizeInfo *)[NSEntityDescription insertNewObjectForEntityForName:@"SizeInfo" inManagedObjectContext:managedObjectContext];
[windowInfo setWidth:[NSString stringWithFormat:@"%f", windowSize.width]];
[windowInfo setHeight:[NSString stringWithFormat:@"%f", windowSize.height]];
NSLog (@"Width = %f, Height = %f", windowInfo.width, windowInfo.height);
I use the following code to fetch the same information:
managedObjectContext = [self managedObjectContext];
NSArray *array = [MyDocument objectsForEntityNamed:@"SizeInfo" inContext:managedObjectContext];
NSLog (@"%@", [array objectAtIndex:0]);
The fetch method on the second line calls the following method in my code:
+ (NSArray *)objectsForEntityNamed:(NSString *)name inContext:(NSManagedObjectContext *)context
{
NSEntityDescription *entity = [NSEntityDescription entityForName:name inManagedObjectContext:context];
NSFetchRequest *req = [[NSFetchRequest alloc] init];
[req setEntity:entity];
NSError *error = nil;
NSArray *array = [context executeFetchRequest:req error:&error];
if (array == nil)
{
NSException *exception = [NSException
exceptionWithName:self
reason:[error localizedDescription]
userInfo:nil];
[req release];
[exception raise];
}
[req release];
return array;
}
When I try to fetch the information, I receive the following error:
*** -[NSArray objectAtIndex:]: index (0) beyond bounds (0)
Upon debugging, it appears I have an initialised array with 0 objects in it.
Does anyone see anything here I might need to fix in order to get things working?
Thanks in advance. Ricky.