开发者

NSMutableArray arrayWithArray: vs. initWithArray:

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?


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜