gpt4 book ai didi

iphone - 错误:访问全局变量和核心数据时,NSArray在枚举时发生了变异

转载 作者:行者123 更新时间:2023-12-01 18:00:49 26 4
gpt4 key购买 nike

我有这段代码,当添加另一个对象时,我将使用这些代码来更新Core Data中的某些值:

//Create new receipt
Receipt *receipt = [[Receipt alloc] init];
receipt.project = self.projectLabel.text;
receipt.amount = self.amountTextField.text;
receipt.descriptionNote = self.descriptionTextField.text;
receipt.business = self.businessNameTextField.text;
receipt.date = self.dateLabel.text;
receipt.category = self.categoryLabel.text;
receipt.paidBy = self.paidByLabel.text;
receipt.receiptImage1 = self.receiptImage1;
//Need to set this to 2
receipt.receiptImage2 = self.receiptImage1;
receipt.receiptNumber = @"99";

int count = 0;
int catCount = 0;

for (Project *p in appDelegate.projects)
{
if ([p.projectName isEqualToString:receipt.project]){
double tempValue = [p.totalValue doubleValue];
tempValue += [receipt.amount doubleValue];
NSString *newTotalValue = [NSString stringWithFormat:@"%.02f", tempValue];
NSString *newProjectName = p.projectName;
//remove entity from Core Data
NSFetchRequest * allProjects = [[NSFetchRequest alloc] init];
[allProjects setEntity:[NSEntityDescription entityForName:@"Project" inManagedObjectContext:appDelegate.managedObjectContext]];
[allProjects setIncludesPropertyValues:NO]; //only fetch the managedObjectID

NSError * error = nil;
NSArray * projectsArray = [appDelegate.managedObjectContext executeFetchRequest:allProjects error:&error];

//Delete product from Core Data
[appDelegate.managedObjectContext deleteObject:[projectsArray objectAtIndex:count]];

NSError *saveError = nil;
[appDelegate.managedObjectContext save:&saveError];

[appDelegate.projects removeObjectAtIndex:count];

NSLog(@"Removed project from Core Data");

//Insert a new object of type ProductInfo into Core Data
NSManagedObject *projectInfo = [NSEntityDescription
insertNewObjectForEntityForName:@"Project"
inManagedObjectContext:appDelegate.managedObjectContext];

//Set receipt entities values
[projectInfo setValue:newProjectName forKey:@"name"];
[projectInfo setValue:newTotalValue forKey:@"totalValue"];

if (![appDelegate.managedObjectContext save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}

NSLog(@"Added Project to Core Data");

Project *tempProject = [[Project alloc] init];
tempProject.projectName = [projectInfo valueForKey:@"name"];
tempProject.totalValue = [projectInfo valueForKey:@"totalValue"];

[appDelegate.projects addObject:tempProject];

}
count++;
}

for (Category *c in appDelegate.categories){

if ([c.categoryName isEqualToString:receipt.category]){

double tempValue = [c.totalValue doubleValue];
tempValue += [receipt.amount doubleValue];
NSString *newTotalValue = [NSString stringWithFormat:@"%.02f", tempValue];
NSString *newCategoryName = c.categoryName;

//remove entity from Core Data
NSFetchRequest * allCategories = [[NSFetchRequest alloc] init];
[allCategories setEntity:[NSEntityDescription entityForName:@"Category" inManagedObjectContext:appDelegate.managedObjectContext]];
[allCategories setIncludesPropertyValues:NO]; //only fetch the managedObjectID

NSError * categoriesError = nil;
NSArray * categoriesArray = [appDelegate.managedObjectContext executeFetchRequest:allCategories error:&categoriesError];

//Delete product from Core Data
[appDelegate.managedObjectContext deleteObject:[categoriesArray objectAtIndex:catCount]];
NSError *categorySaveError = nil;
[appDelegate.managedObjectContext save:&categorySaveError];

[appDelegate.categories removeObjectAtIndex:catCount];

NSLog(@"Removed category from Core Data");
NSError * error = nil;
//Insert a new object of type ProductInfo into Core Data
NSManagedObject *categoryInfo = [NSEntityDescription
insertNewObjectForEntityForName:@"Category"
inManagedObjectContext:appDelegate.managedObjectContext];

//Set receipt entities values
[categoryInfo setValue:newCategoryName forKey:@"name"];
[categoryInfo setValue:newTotalValue forKey:@"totalValue"];

if (![appDelegate.managedObjectContext save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}

NSLog(@"Added Category to Core Data");

Category *tempCategory = [[Category alloc] init];
tempCategory.categoryName = [categoryInfo valueForKey:@"name"];
tempCategory.totalValue = [categoryInfo valueForKey:@"totalValue"];

[appDelegate.categories addObject:tempCategory];
}
catCount++;

}

此代码给出错误:

“...被枚举时发生了变异”。

谁能解释为什么?另外,是否有更好的方法来完成我要达到的目标?

最佳答案

您看到的错误是准确的。问题是您要在迭代集合时对其进行变异(更改)。基本上,您正在做以下形式的事情:

for (Project *p in appDelegate.projects) {
...
[p addObject: X]
}

这是不允许的。

一种简单的解决方案是对要添加的对象进行新的收集,然后将其添加到循环外的原始容器中。就像是:
NSMutableArray *array = [NSMutableArray array];
for (Project *p in appDelegate.projects) {
...
[array addObject:X];
}
[p addObjects:array];

顺便说一句,您是否在Google上输入了错误文本“被枚举时被变异”?如果您只是通过谷歌搜索找不到这个常见问题的答案,我会感到惊讶。

此外,发布错误消息时,发布完整的行而不只是其中的一部分很有帮助。

关于iphone - 错误:访问全局变量和核心数据时,NSArray在枚举时发生了变异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10188467/

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