reason: '* -[NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'

原创 2015年07月08日 13:20:36

错误提示 :
* Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object’

错误分析:
看reason:后面的异常说明:意思是我把一个可变量对应的方法让一个不可变量来调用
mutating method(可变量对应的方法):是那些在创建后可以被更改的变量所拥有的method,比如NSMutableArray,NSMutableDictionary 等
immutable object(不可改变的变量):就是那些被创建后不能被改变的变量:比如 NSArray NSDictionary等;
我的Exception被报出来是因为我有一行错误代码
[listAry removeObjectAtIndex:indexPath.row];
这里,我的array是从一个不可改变的变量类型为NSDictionary得到的,
代码为:NSMutableArray *listAry = [self.listDic objectForKey:@”list”];
所以,listAry这里也是一个不可改变量(即使我声明用的是NSMutableArray),但显然removeObjectAtIndex是一个改变变量的操作,所以这是不允许的。

错误解决前:
NSMutableArray *listAry = [[NSMutableArray alloc] initWithCapacity:0];
listAry = [self.listDic objectForKey:@"list"];

错误解决后:

NSMutableArray *listAry = [[NSMutableArray alloc] initWithCapacity:0];
[listAry addObjectsFromArray:[self.listDic objectForKey:@"list"]];

再啰嗦一句原因:就是因为这个变量之前被赋值了,而无法把一个可变的方法固执给一个不可变的对象。

如有不正确,还望批评指正….

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

iOS程序崩溃,提示[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object’的解决办法

iOS程序崩溃,提示[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object’的解决办法在做相册Demo...

activemq报错:Failed to start ActiveMQ JMS Message Broker. Reason: java.io.EOFException: Chunk stream d

2016-10-11 17:16:25,452 | ERROR | Failed to start ActiveMQ JMS Message Broker. Reason: java.io.EOFEx...

windows 下安装部分gem时出现 Could not create Makefile due to some reason 的解决办法

现象描述: 如果你安装某个需要本地编译代码的gem 时出现了类似如下的错误,则符合本文章所谈论的情况: C:/Develop/Ruby193/bin/ruby.exe extconf.rb ch...

Follow up to “Android graphics true facts”, or The Reason Android is Laggy

Edit: +Dianne Hackborn has written a response to this post. She clarifies things about Android that ...

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<YHTargetTableViewCell

iOS开发使用StoryBoard拖的控件报这个错:Terminating app due to uncaught exception 'NSUnknownKeyException', reason:...

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArra

这个错误说的是一个不可变数组负值给了一个可变的数组。yo

javax.jms.JMSException: Failed to build body from bytes. Reason: java.io.IOException

使用activeMQ发送ObjectMessage类型时,遇到javax.jms.JMSException: Failed to build body from bytes. Reason: java...

VMware虚拟机出现Reason: Failed to lock the file

打开VMware出现Cannot open the disk *.vmdk or one of the snapshot disks it depends on.Reason: Failed to l...

Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml, reason: Connect

Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml, reason: Connecti...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)