Autorelease vs. release

后端 未结 1 1727
抹茶落季
抹茶落季 2021-01-01 01:52

When I need an array for temporary use, what\'s the difference between these:

1:

NSMutableArray *stuff = [[NSMutableArray alloc] init];
// use the ar         


        
相关标签:
1条回答
  • 2021-01-01 02:09

    Number 2 is likely the best choice in most cases.

    Number 1 has the chance of losing the release at some point down the line, for whatever reason, but it does release the array immediately, which in memory-starved environments can be useful.

    Number 3 is basically a verbose equivalent of number 2, but it does come in handy if you want to use an initWith* that doesn't have a corresponding arrayWith*.

    Note: If you are memory-starved, such as in an expensive loop where you need a fresh array for each iteration; don't release and allocate new arrays; just use -removeAllObjects and recycle the array.

    0 讨论(0)
提交回复
热议问题