Objective-c 属性和内存管理
只是一个简单的问题: 我有一个数组,将其命名为 sourceArray。该数组中有字典。 我有一个保留属性,用于保存数组中的一项。 当我从数组中设置属性时,它工作正常。 但是,当我将其设置为其他项目并返回到第一个项目时,我收到了 bad_access 异常,因为第一个项目已被释放。
我的问题是为什么?在源数组中,项目被保留,当我设置它保留的属性时。当我再次设置它时,设置器会释放旧值并保留新值。我猜...但显然这样不行。
例子:
[self setProperty:[sourceArray objectAtIndex:0]];
[self setProperty:[sourceArray objectAtIndex:1]];
[self setProperty:[sourceArray objectAtIndex:0]]; ---> Exception
Just a quick question:
I've got an array, name it sourceArray. This array has dictionaries in it.
I've got a retained property, which is used to hold one item from the array.
When I set the property from the array it works fine.
But, when I set it with an other item, and back to the first, I got a bad_access exception, since the first item has been released.
My question is why? In the source array, items are retained, and when I set the property it retains to. When I set it again, the setter release the old value, and retain the new. I guess... But apparently it doesn't work in this way.
Example:
[self setProperty:[sourceArray objectAtIndex:0]];
[self setProperty:[sourceArray objectAtIndex:1]];
[self setProperty:[sourceArray objectAtIndex:0]]; ---> Exception
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我明白了。
在其他地方,我有一句话:
看起来当我使用set方法时,旧值被释放,无论它没有被保留(因为在第一次,我没有使用set方法)。
我的错...
I've got it.
In other place, I've a line:
It looks like when I use the set method, the old value get released, no matter that it hasn't been retained (since in the first time, I didn't use the set method).
My fault...