开发者

NSMUrableArray alloc and init

I used codes below and set the breakpoint at a1, a2

NSMutableArray *aArray;

.....

@property (nonatomic,retain)    NSMutableArray *aArray;

......

NSMutableArray* a=[[NSMutableArray alloc]init] ;


for(int i=1;i<=31;i++)
    [a  addObject:[NSNumber numberWithInt:i]];
aArray=a; 


[a release];// a1

int i=0;// a2

the amount of objects in aArray is 31 but afeter the line [a release], the amount changes to 0

As I know 'release' only make the retain counter -1, but why it also removes 开发者_StackOverflowall objects in the MutableArray?

Welcone any comment

Thanks

interdev


With [a release], the reference count for the array becomes 0 and therefore, the array gets deallocated. When that happens, the array removes all objects from its contents in order to release them (because it has retained them before when they were added to the array).


use following statement;

aArray = [a retain];

If you use retain, aArray will not change.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜