Collection <__NSArrayM: 0x7fa1f2711910> 在枚举时发生了变异
Collection <__NSArrayM: 0x7fa1f2711910> was mutated while being enumerated
有许多标题相似的问题,但 none 它们对我有帮助。
但我可以解决这个问题
问我的问题。 因为正在终止
当我用数组添加、删除字符串 object 时。
错误:
由于未捕获的异常而终止应用程序 'NSGenericException',
原因:'*** Collection <__NSArrayM: 0x7fa1f2711910> 在枚举时发生了变异。'
第一次抛出调用堆栈:
(
0 CoreFoundation 0x00000001087b8c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000108422bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001087b85c4 __NSFastEnumerationMutationHandler + 132
3 0x0000000107e24cf5 -[ProfessionalRegSecond tableView:didDeselectRowAtIndexPath:] + 773
4 UIKit 0x0000000109759d71 -[UITableView _deselectRowAtIndexPath:animated:notifyDelegate:] + 347
5 UIKit 0x000000010974deea -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 187
6 UIKit 0x000000010968062c _applyBlockToCFArrayCopiedToStack + 314
7 UIKit 0x00000001096804a6 _afterCACommitHandler + 533
8 CoreFoundation 0x00000001086ebca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
9 CoreFoundation 0x00000001086ebc00 __CFRunLoopDoObservers + 368
10 CoreFoundation 0x00000001086e1a33 __CFRunLoopRun + 1123
11 CoreFoundation 0x00000001086e1366 CFRunLoopRunSpecific + 470
12 GraphicsServices 0x000000010d6c8a3e GSEventRunModal + 161
13 UIKit 0x000000010965c900 UIApplicationMain + 1282
14 0x0000000107de14cf main + 111
15 libdyld.dylib 0x000000010b585145 start + 1
)
libc++abi.dylib: 以 NSException
类型的未捕获异常终止
我正在尝试做什么:
我需要这样的数组,当我 select 单元格时,我想将 object 添加到数组中,当我删除 select 行时,我想删除它数组中的行,因此结果数组将只有 selected 条目。如何做到这一点?
这是我的代码
@implementation Professional
{
NSMutableArray *updatedLocalityArray; // alloc,init did in viewDidLoad
NSString *toCheck;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",tableViewCell.textLabel.text);
toCheck = tableViewCell.textLabel.text;
[updatedLocalityArray addObject:toCheck];
NSLog(@"%@ *****", updatedLocalityArray);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",tableViewCell.textLabel.text);
for (NSString *test in updatedLocalityArray)
{
if ([test isEqualToString:tableViewCell.textLabel.text])
{
[updatedLocalityArray removeObject:test];
NSLog(@"%@ *****", updatedLocalityArray);
}
}
}
您不能在使用 for...in...
控制语句循环遍历数组时修改数组。
尝试:
NSArray *compareArray = [updatedLocalityArray copy];
for (NSString *test in compareArray)
// ...
您无法在 NSMutableArray
迭代时从中删除项目。
有几种解决方法:
- 迭代数组的副本
或
- 使用基于索引的
for
循环而不是 for each
语法。
不复制数组可以节省分配和几个 CPU 周期:
for (int i = updatedLocalityArray.count-1 ; i >= 0 ; i--)
{
NSString *test = updatedLocalityArray[i];
if ([test isEqualToString:tableViewCell.textLabel.text])
{
[updatedLocalityArray removeObjectAtIndex:i];
NSLog(@"%@ *****", updatedLocalityArray);
}
}
在你的表格视图取消选择方法中,你写道:
for (NSString *test in updatedLocalityArray)
{
if ([test isEqualToString:tableViewCell.textLabel.text])
{
[updatedLocalityArray removeObject:test];
NSLog(@"%@ *****", updatedLocalityArray);
}
}
在该代码中,您通过 updatedLocalityArray
进行枚举,如果找到匹配项,您将从同一数组中删除该对象,此逻辑会导致此处出现问题。为了解决这个问题,您需要创建本地副本并在其中进行更改并在枚举后将其分配回原始数组。
NSMutableArray *tempArray = [updatedLocalityArray mutableCopy];
for (NSString *test in updatedLocalityArray)
{
if ([test isEqualToString:tableViewCell.textLabel.text])
{
[tempArray removeObject:test];
NSLog(@"%@ *****", tempArray);
}
}
updatedLocalityArray = tempArray;
如果你当前的数组是A,你可以复制另一个相同的数组叫B,然后枚举B,操作A,就不会崩溃了。
如果你当前的数组是A
,你可以复制另一个相同的数组叫B
,然后枚举B
并操作A
,就不会了崩溃。
您不能在枚举容器时修改容器。
NSMutableArray *tempArray = someArray.mutableCopy;
有许多标题相似的问题,但 none 它们对我有帮助。
但我可以解决这个问题
错误:
由于未捕获的异常而终止应用程序 'NSGenericException', 原因:'*** Collection <__NSArrayM: 0x7fa1f2711910> 在枚举时发生了变异。'
第一次抛出调用堆栈:
(
0 CoreFoundation 0x00000001087b8c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000108422bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001087b85c4 __NSFastEnumerationMutationHandler + 132
3 0x0000000107e24cf5 -[ProfessionalRegSecond tableView:didDeselectRowAtIndexPath:] + 773
4 UIKit 0x0000000109759d71 -[UITableView _deselectRowAtIndexPath:animated:notifyDelegate:] + 347
5 UIKit 0x000000010974deea -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 187
6 UIKit 0x000000010968062c _applyBlockToCFArrayCopiedToStack + 314
7 UIKit 0x00000001096804a6 _afterCACommitHandler + 533
8 CoreFoundation 0x00000001086ebca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
9 CoreFoundation 0x00000001086ebc00 __CFRunLoopDoObservers + 368
10 CoreFoundation 0x00000001086e1a33 __CFRunLoopRun + 1123
11 CoreFoundation 0x00000001086e1366 CFRunLoopRunSpecific + 470
12 GraphicsServices 0x000000010d6c8a3e GSEventRunModal + 161
13 UIKit 0x000000010965c900 UIApplicationMain + 1282
14 0x0000000107de14cf main + 111
15 libdyld.dylib 0x000000010b585145 start + 1
)
libc++abi.dylib: 以 NSException
类型的未捕获异常终止我正在尝试做什么:
我需要这样的数组,当我 select 单元格时,我想将 object 添加到数组中,当我删除 select 行时,我想删除它数组中的行,因此结果数组将只有 selected 条目。如何做到这一点?
这是我的代码
@implementation Professional
{
NSMutableArray *updatedLocalityArray; // alloc,init did in viewDidLoad
NSString *toCheck;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",tableViewCell.textLabel.text);
toCheck = tableViewCell.textLabel.text;
[updatedLocalityArray addObject:toCheck];
NSLog(@"%@ *****", updatedLocalityArray);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",tableViewCell.textLabel.text);
for (NSString *test in updatedLocalityArray)
{
if ([test isEqualToString:tableViewCell.textLabel.text])
{
[updatedLocalityArray removeObject:test];
NSLog(@"%@ *****", updatedLocalityArray);
}
}
}
您不能在使用 for...in...
控制语句循环遍历数组时修改数组。
尝试:
NSArray *compareArray = [updatedLocalityArray copy];
for (NSString *test in compareArray)
// ...
您无法在 NSMutableArray
迭代时从中删除项目。
有几种解决方法:
- 迭代数组的副本
或
- 使用基于索引的
for
循环而不是for each
语法。
不复制数组可以节省分配和几个 CPU 周期:
for (int i = updatedLocalityArray.count-1 ; i >= 0 ; i--)
{
NSString *test = updatedLocalityArray[i];
if ([test isEqualToString:tableViewCell.textLabel.text])
{
[updatedLocalityArray removeObjectAtIndex:i];
NSLog(@"%@ *****", updatedLocalityArray);
}
}
在你的表格视图取消选择方法中,你写道:
for (NSString *test in updatedLocalityArray)
{
if ([test isEqualToString:tableViewCell.textLabel.text])
{
[updatedLocalityArray removeObject:test];
NSLog(@"%@ *****", updatedLocalityArray);
}
}
在该代码中,您通过 updatedLocalityArray
进行枚举,如果找到匹配项,您将从同一数组中删除该对象,此逻辑会导致此处出现问题。为了解决这个问题,您需要创建本地副本并在其中进行更改并在枚举后将其分配回原始数组。
NSMutableArray *tempArray = [updatedLocalityArray mutableCopy];
for (NSString *test in updatedLocalityArray)
{
if ([test isEqualToString:tableViewCell.textLabel.text])
{
[tempArray removeObject:test];
NSLog(@"%@ *****", tempArray);
}
}
updatedLocalityArray = tempArray;
如果你当前的数组是A,你可以复制另一个相同的数组叫B,然后枚举B,操作A,就不会崩溃了。
如果你当前的数组是A
,你可以复制另一个相同的数组叫B
,然后枚举B
并操作A
,就不会了崩溃。
您不能在枚举容器时修改容器。
NSMutableArray *tempArray = someArray.mutableCopy;