Why setting to nil after releasing?

前端 未结 4 692
不思量自难忘°
不思量自难忘° 2021-01-03 16:08

I came across this method:

-(void) someMethod {
    NSMutableArray *anArray = [[NSMutableArray alloc] init]; 
    // Do stuff with anArray ... 
    [anArray          


        
4条回答
  •  隐瞒了意图╮
    2021-01-03 16:57

    In this case, it is a pointless waste of key strokes because the variable anArray goes out of scope immediately.

    In other cases, where the variable stays in scope for a while after you release the object its pointing to, it is a good idea, because, if you accidentally dereference it, you will get a EXC_BAD_ACCESS which is easy to spot, and if you send a message to it, it will be ignored (except for returning nil / 0).

提交回复
热议问题