retain count in iphone

前端 未结 3 1788
小蘑菇
小蘑菇 2021-01-24 04:15

I have used [anArray retainCount] to get the retain count of array..(i know this should not be used but i am using just for learning retain concept)

Follow

3条回答
  •  既然无缘
    2021-01-24 04:27

    Please do yourself a favor and don't look at retainCount trying to learn how the memory management rules work. Instead refer to the friendly Apple Memory Management Guide.

    In your examples:

     NSArray  *anArray =[[NSArray alloc]init];
    

    You have allocated "anArray" (by calling alloc), so you are responsible for calling release.

    anArray=[str componentsSeparatedByString:@","];
    

    Now, you have obtained a new object (leaking the original, as seand said). This time, you do not own the object (because componentsSeparatedByString does not have alloc or copy in its name), so you must not release it.

    Don't worry about what the retainCount is; tend to your own knitting and release objects that you should and don't release objects you don't own.

提交回复
热议问题