gpt4 book ai didi

ios - 核心数据 : `EXC_BAD_ACCESS` When Accessing Objects in Relationship

转载 作者:行者123 更新时间:2023-11-29 13:47:45 24 4
gpt4 key购买 nike

我有一对多的映射关系,名称匹配和局。

match <-->> innings
  • match 有字段名称、id 等。
  • innings 有字段编号。

我能够获得 match 属性。我在 TableListControllerMatchList 中创建了一个新匹配。我看到表中提供了 Matchinnings 的数据。现在,我单击在表中创建的行。

但是当我执行 [match matchinnings], 时,我得到一个 NSSet* inningSet。我能够从 inningSet 中得到两个对象 inningA 和 inningB。当我尝试调用 [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 故障。

非常感谢您的帮助。

我尝试迭代返回的集合。 [a addObject:inningsObject] 行仍然出现“EXC_BAD_ACCESS”错误。 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;
}

最佳答案

当然,如果您在 CoreData 中将某些东西建模为 Integer 并提供自定义类来实现它。该属性是一个对象:NSNumber

例子:

.h:

// attributes
@property (nonatomic, retain) NSNumber *version;

// derived attributes
@property (nonatomic, readonly) NSInteger versionIntegerValue;
@property (nonatomic, readonly) NSString *versionStringValue;

.m:

@dynamic version;

- (NSInteger)versionIntegerValue {
return [self.version integerValue];
}

- (NSString *)versionStringValue {
return [NSString stringWithFormat:@"v%d", [self.version integerValue]];
}

关于ios - 核心数据 : `EXC_BAD_ACCESS` When Accessing Objects in Relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6572605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com