开发者

iPhone : copyWithZone : releasing an object before its return?

开发者 https://www.devze.com 2023-02-03 07:58 出处:网络
I read in the apple documentation about copyWithZone : \"The returned object is implicitly retained by the sender, who is responsible for releasing it\".

I read in the apple documentation about copyWithZone : "The returned object is implicitly retained by the sender, who is responsible for releasing it". But... How may I release something I return... I'm going crazy !

Sample of code :

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

        return obj; // implicit retain
    }

Where should be the told release ? I retain twice ? Uhhh...


The sender is responsible for releasing. That means whoever calls your copy method takes ownership, i.e.:

MyObject *obj = ...
MyObject *aCopy = [obj copy];
... do stuff with aCopy
[aCopy release];
0

精彩评论

暂无评论...
验证码 换一张
取 消