NCSFDictionary,发送到不可变对象的变异方法
我刚刚开始进入 Objective-C 领域,并且正在慢慢掌握这一切。我一直致力于解压一个 NSMutableArray 文件,然后使用该数组在我的模型中进行初始化。该数组充满了各种 NSMutableDicationary。据我所知,它将把这些字典添加为不可变的,所以我继续复制常规字典并将它们放入可变字典中并删除旧的字典。该解决方案似乎适用于除第一个实例之外的所有实例。
我不知道为什么它对除了第一个之外的所有人都有效。
这是我初始化所有内容的方式
-(id) initWithList:(NSMutableArray *)savedList
{
self = [super init];
if (self)
{
int size=0;
serverList=[[NSMutableArray alloc] initWithArray:savedList copyItems:YES];
size=[serverList count];
for(int i=0;i<size;i++)
{
loginList=[NSMutableDictionary dictionaryWithDictionary:[serverList objectAtIndex:i]];
[serverList addObject:loginList];
[serverList removeObjectAtIndex:i];
}
}
return self;
}
这是引发错误的代码,该值正在从表视图中的复选框中读取并传递到此处以更改值。
-(void)setMount:(int)row value:(NSNumber*)boolAsNumber
{
[[serverList objectAtIndex:row] setObject:boolAsNumber forKey:@"mountshare"];
}
这是我尝试更改第一个元素时显示的错误
2010-12-01 13:38:54.445 Network Share[35992:a0f] *** -[NSCFDictionary setObject:forKey:]: mutating method sent to immutable object
感谢您的帮助。如果有更好的方法请告诉我。
I have just started to jump into the realm of Objective-C and am slowly getting it all. I have been working on unarchiving a file that was a NSMutableArray and then initializing in my model with that array. The array is filled with various NSMutableDicationary's. From what I have seen it will add those dictionaries as non-mutable, so I went ahead and copied the regular and put them in a mutable and remove the old one. This solution seems to work for every instance except the very first.
I am at a loss as to why it would work for all but the first.
Here is how I am initializing it all
-(id) initWithList:(NSMutableArray *)savedList
{
self = [super init];
if (self)
{
int size=0;
serverList=[[NSMutableArray alloc] initWithArray:savedList copyItems:YES];
size=[serverList count];
for(int i=0;i<size;i++)
{
loginList=[NSMutableDictionary dictionaryWithDictionary:[serverList objectAtIndex:i]];
[serverList addObject:loginList];
[serverList removeObjectAtIndex:i];
}
}
return self;
}
Here is the code that is throwing the error, The value is being read off of a checkbox in a tableview and passed here to change the value.
-(void)setMount:(int)row value:(NSNumber*)boolAsNumber
{
[[serverList objectAtIndex:row] setObject:boolAsNumber forKey:@"mountshare"];
}
Here is the error that it shows when I try and change the first element
2010-12-01 13:38:54.445 Network Share[35992:a0f] *** -[NSCFDictionary setObject:forKey:]: mutating method sent to immutable object
Thanks for your help. If there is a better way please let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此循环代码是错误的:
当删除对象时,数组会重新编号。处理完索引 0 处的第一个对象后,原始的第二个对象将成为索引 0 处的第一个对象,但
i
现在设置为索引 1,这就是原始第三个对象所在的位置!这意味着您只处理原始数组中的替代项目,而第二个、第四个等项目永远不会被交换,这就是您看到错误的原因。解决此问题的一种方法是将
objectAtIndex:
和removeObjectAtIndex:
调用中的“i”替换为“0”,这样您始终会从前面取出项目数组的。另一种解决方案是创建一个单独的 newServerList 数组并将新对象插入其中。在循环结束时,释放旧的 serverList 并将变量设置为指向 newServerList。
This loop code is wrong:
When you remove an object, the array is renumbered. After you've processed the 1st object at index 0, the original 2nd object is becoming the 1st object at index 0, but
i
is now set to index 1, which is where the original 3rd object is! This means you're only processing alternate items from the original array, and the 2nd, 4th, etc items never get swapped, and that's why you get the errors you're seeing.One way to solve this would be to replace the "i" in the
objectAtIndex:
andremoveObjectAtIndex:
calls with "0", so you're always taking items off the front of the array.The alternate solution would be to create a separate
newServerList
array and insert your new objects into that. At the end of the loop, release the old serverList and set the variable to point to newServerList.你的索引乱了。一旦您删除了索引 0 处的对象,下一个对象就会取代它,并且您永远不会替换它,因为您随后会继续使用索引 1。
-->那里仍然有不可变的东西。请记住,永远不要从同时循环的可变数组中删除对象。
您可以稍微压缩一下代码:
与您的问题无关:如果您期望那里有一个不可变数组,则参数显然是错误的(
NSMutableArray
);如果您以这种方式创建 serverList,则不需要深层复制 (copyItems:YES)。Your indexes are messed up. As soon as you remove the object at index 0, the next one will take it's place and you will never replace that, because you then carry on with index 1.
--> still got the immutable there. Remember to never remove objects from a mutable array you are looping through at the same time.
You could condense the code a bit:
Unrelated to your problem: the argument is obviously wrong (
NSMutableArray
), if you expect an immutable array there; and if you create your serverList that way, there is no need for a deep copy (copyItems:YES).