iPhone:copyWithZone:在返回之前释放一个对象?

时间:2011-01-08 01:27:23

标签: iphone release return retain copywithzone

我在关于copyWithZone的苹果文档中读到:“返回的对象由发送者隐式保留,发送者负责释放它”。 但是......我怎么能释放一些我回来的东西......我疯了!

代码示例:

    - (id)copyWithZone:(NSZone *)zone {
        MyObject* obj = [[[self class] allocWithZone:zone] init]; // explicit retain
        [obj fillTheObj];

        return obj; // implicit retain
    }

告诉发布应该在哪里? 我保留两次?唔...

1 个答案:

答案 0 :(得分:1)

发件人负责发布。这意味着无论谁调用你的复制方法都需要所有权,即:

MyObject *obj = ...
MyObject *aCopy = [obj copy];
... do stuff with aCopy
[aCopy release];
相关问题