是"removeAllObjects"吗?应该释放? [英] Is "removeAllObjects" supposed to release?

查看:123
本文介绍了是"removeAllObjects"吗?应该释放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定开始使用removeAllObjects,以便可以重用我的某些NSMutableArrays.但是,我发现对removeAllObjects的调用导致数组被释放,因此在第二次显示viewController时崩溃.

I decided to start using removeAllObjects so that I could re-use some of my NSMutableArrays. However, I am finding that the call to removeAllObjects causes the array to get released and so it crashes when the viewController is displayed for the second time.

我在网上查看过,似乎有些人说removeAllObjects只是清除数组,而其他人说它也释放了数组.哪个是对的?它似乎为我释放了,但我发现这很奇怪,并且希望行为只是释放数组中的对象.文档中对此没有任何警告,我也觉得很奇怪.

I've looked on the web and it seems that some people are saying that removeAllObjects just clears their array, while others are saying that it also releases the array. Which is true? It appears to release for me, but I find that weird and would expect the behaviour to be just to release the objects within the array. Nothing in the documentation warns of this, which I also find strange.

打开NSZombies后,我得到的只是:

With NSZombies turned on, all I get back is:

  • -[__ NSArrayM removeAllObjects]:发送到已释放实例0x625e4e0的消息
  • -[__NSArrayM removeAllObjects]: message sent to deallocated instance 0x625e4e0

基本上告诉我,由于数组是由于removeAllObjects而释放的,因此无法第二次调用removeAllObjects ...哎呀!

Basically telling me that because the array was released due to the removeAllObjects, it can't call removeAllObjects second time round... Argh!

任何人都可以帮忙吗?

谢谢!

推荐答案

好吧,我当时是个白痴!

Okay so I was being an idiot!

我在做:

[myArray removeAllObjects];
myArray = someOtherObject.someArray;

因此将指针重置为其他对象.我现在将其更改为:

therefore resetting the pointer to some other object. I've now changed it to this:

[self.myArray removeAllObjects];
[self.myArray addObjectsFromArray:someOtherObject.someArray];

现在可以了.我是个白痴!抱歉,谢谢您的帮助!

And it's now okay. I'm an idiot! Sorry all, thanks for your help!

这篇关于是"removeAllObjects"吗?应该释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆