gpt4 book ai didi

iphone - 'NSRangeException',原因 : '* -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'

转载 作者:行者123 更新时间:2023-11-29 11:06:21 25 4
gpt4 key购买 nike

我正在尝试删除一些项目,但我收到了这个 NSException:

'NSRangeException',原因:'* -[__NSArrayM objectAtIndex:]:索引 2 超出范围 [0 .. 1]'

这是我的代码:

-(void)deletePressed:(id)sender {

if (data.count > 0) {

NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents/Galeria/"];

NSFileManager *manager = [NSFileManager defaultManager];

for (NSIndexPath *indexPath in itensSelecionados) {

NSString *result = [path stringByAppendingFormat:@"%@", [[manager contentsOfDirectoryAtPath:path error:nil] objectAtIndex:indexPath.row]];

[manager removeItemAtPath:result error:nil];

}

[self viewWillAppear:YES];

}}

有人可以帮忙吗?

最佳答案

您不能从正在迭代的数组中删除对象。
可能没有适合您的解决方案。

一种是使用一个额外的可变数组来保存所有应该删除的对象,然后遍历它并从原始数组中删除对象:

-(void)deletePressed:(id)sender {
if (data.count > 0) {
NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents/Galeria/"];
NSFileManager *manager = [NSFileManager defaultManager];
NSMutableArray *filesToDelete = [NSMutableArray array];

// Build a list of files to delete
for (NSIndexPath *indexPath in itensSelecionados) {
NSString *result = [path stringByAppendingFormat:@"%@", [[manager contentsOfDirectoryAtPath:path error:nil] objectAtIndex:indexPath.row]];
[filesToDelete addObject:result];
}

// Actually delete the files
for (NSString *indexPathString in filesToDelete) {
[manager removeItemAtPath:indexPathString error:nil];
}

// Why do you call viewWillAppear directly ??
[self viewWillAppear:YES];
}
}

编辑
根据 Thiago 的建议,在第二次迭代中将 NSIndexPath 修复为 NSString

关于iphone - 'NSRangeException',原因 : '* -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13444437/

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