我在创建 TableView 时犯了一个错误class
,并且不小心保留了我@property
定义copy
它时的样子:
@property (copy, nonatomic) NSMutableArray *words;
我“正确”地初始化了数组:(注意这是第三次尝试,所以请忽略我没有使用 mutableCopy 和其他更好的方法的事实)
NSArray *fixedWords = @[@"Eeny", @"Meeny", @"Miny", @"Moe", @"Catch", @"A", @"Tiger", @"By", @"His", @"Toe"];
NSMutableArray *mutWords = [[NSMutableArray alloc] initWithArray:fixedWords];
self.words = mutWords;
但是,当我后来重新排序数组时,它在 removeObjectAtIndex 行上崩溃了:
id object = [self.words objectAtIndex:fromIndexPath.row];
NSUInteger from = fromIndexPath.row;
NSUInteger to = toIndexPath.row;
[self.words removeObjectAtIndex:from];
带有错误消息
unrecognized selector sent to instance
花了很多时间才弄清楚这是因为副本意味着分配 NSMutableArray 会导致创建标准(非可变)NSArray。谁能解释为什么这是正确的行为?