У меня есть отношение отображения один ко многим с соответствием имени и подачей.
match <-->> innings
match
имеет имя поля, идентификатор и т. Д. innings
имеет номер поля.
Я могу получить match
свойства.Я создаю новый матч в MatchList
в TableListController
.Я вижу данные для Match
и innings
доступны в таблице.Теперь я щелкаю строку, созданную в таблице.
Но когда я делаю [match matchinnings],
, я получаю NSSet* inningSet
.Я могу получить два объекта inningA и inningB от inningSet.Когда я пытаюсь вызвать [inningA number]
, я получаю сообщение об ошибке.
Ниже приведен мой метод NSFetchResultsController:
- (NSFetchedResultsController *)fetchedResultsController {
// Set up the fetched results controller if needed.
//NSLog(@"Inside fetchResultsController ");
if (fetchedResultsController == nil) {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setReturnsObjectsAsFaults:NO];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Match" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"matchinnings"]];
[fetchRequest setIncludesSubentities:YES];
[fetchRequest setResultType:NSManagedObjectResultType];
//[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"matchinnings.number", nil]];
//[fetchRequest valueForKeyPath:@"matchinnings.number"];
//[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"matchinnings", @"number", nil]];
//[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"matchinnings.number"]];
//[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"matchinnings", @"matchinnings.number", nil]];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
}
return fetchedResultsController;
}
В моем классе MatchContextMO я получаю сообщение об ошибке EXC_BAD_ACCESS
настрока:
inningsArray = [newSet allObjects];
... в этом методе:
-(NSArray *)determineInningsOrder {
NSArray* array = nil;
NSSet *newSet = [self.match valueForKey:@"matchinnings"];
NSLog(@"Size of set %d", [newSet count]);
NSArray *inningsArray = nil;
@try {
inningsArray = [newSet allObjects];
}
@catch (NSException *exception) {
NSLog(@"Exception in matchinnings %@", [exception reason]);
}
Innings *inningA = [inningsArray objectAtIndex:0];
Innings *inningB = [inningsArray objectAtIndex:1];
if ([inningA isKindOfClass:Innings.class])
NSLog(@"inningA is of type Innings");
Innings* temp;
NSNumber *numberA = [inningA valueForKey:@"number"];
NSLog(@"numberA %d", [numberA intValue]);
if ([numberA intValue] == 2) {
temp = inningA;
inningA = inningB;
inningB = temp;
}
array = [NSArray arrayWithObjects:inningA, inningB, nil];
return array;
}
Я пытаюсь выяснить это за последнюю неделю.Это больше похоже на ошибки CoreData.
Ваша помощь очень ценится.
Я попытался перебрать возвращенный набор.По-прежнему получена ошибка «EXC_BAD_ACCESS» в строке [a addObject:inningsObject]
.NSLog размер набора говорит, хотя 2.
-(NSArray *)determineInningsOrder {
NSArray* array = nil;
if (!self.match) {
NSLog(@"Match is null");
return nil;
}
NSMutableSet *newSet = [self.match valueForKey:@"matchinnings"];
NSLog(@"Size of set %d", [newSet count]);
//NSSet *inningsSet = [self.match matchinnings];
NSArray *inningsArray = nil;
NSEnumerator *fastEnumerator = [newSet objectEnumerator];
id inningsObject;
NSMutableArray *a = [[NSMutableArray alloc] initWithCapacity:[newSet count]];
while ((inningsObject = [fastEnumerator nextObject])) {
[a addObject:inningsObject];
}
Innings *inningA = [a objectAtIndex:0];
Innings *inningB = [a objectAtIndex:1];
[a release];
if ([inningA isKindOfClass:Innings.class])
NSLog(@"inningA is of type Innings");
NSNumber *numberA = [inningA valueForKey:@"number"];
NSLog(@"numberA %d", [numberA intValue]);
if ([numberA intValue] == 2) {
temp = inningA;
inningA = inningB;
inningB = temp;
}
array = [NSArray arrayWithObjects:inningA, inningB, nil];
return array;
}