gpt4 book ai didi

objective-c - 将 NSNumber 对象添加到 NSMutableArray

转载 作者:行者123 更新时间:2023-12-04 05:12:29 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





NSMutableArray can't be added to

(2 个回答)


8年前关闭。




这段代码会导致错误,但它应该是正确的。

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *rowNumber = [[NSNumber alloc] initWithInt:indexPath.row];
[genreArray addObject:rowNumber];
[self.tableView reloadData];
}
genreArrayNSMutableArray
但是在触摸单元格时我收到此错误
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
*** First throw call stack:
(0x1c9c012 0x10d9e7e 0x1c9bdeb 0x1d1c9a5 0x1d1c8b0 0x7b4c 0xcd285 0xcd4ed 0xad75b3 0x1c5b376 0x1c5ae06 0x1c42a82 0x1c41f44 0x1c41e1b 0x1bf67e3 0x1bf6668 0x1dffc 0x271d 0x2645)
libc++abi.dylib: terminate called throwing an exception

有没有人知道为什么这不起作用?

最佳答案

因为你的数组是不可变的......
检查您实例化的代码 genreArray : 类型声明和实例化都必须使用 NSMutableArray 完成

//valid declaration + instantiation
NSMutableArray *genreArray = [NSArray array];

//valid at compile-time : genreArray is supposed to be mutable
//however will result in runtime error : genreArray is actually immutable
[genreArray addObject:anObject];

关于objective-c - 将 NSNumber 对象添加到 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14724038/

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