NSMutableArray arrayWithArray: vs. initWithArray:

眉间皱痕 提交于 2019-12-09 18:49:18

问题


These both work in my app without any noticeable difference:

1)

theArray = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:theData]];

2)

theArray = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:theData]];
[theArray retain];

However, are they really equivalent? (1) has an alloc statement, whereas (2) does not. Is one preferable over the other?


回答1:


The effect is the same. But (2) is less efficient (a convenient method = alloc + init + autorelease).

  1. alloc → init
  2. alloc → init → autorelease → retain

The preferred way is not to copy the array.

theArray = [[NSKeyedUnarchiver unarchiveObjectWithData:theData] retain];

BTW, I notice that you have been asking a lot of basic questions about iPhone OS development. Please go through the tutorials on these first.



来源:https://stackoverflow.com/questions/3749657/nsmutablearray-arraywitharray-vs-initwitharray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!