开发者

retain will allocate Memory in RAM?

the follw开发者_运维知识库ing will allocate memory in RAM ....?

NSArray *obj = [[NSArray arrayWithObjects: @"Hai", @"iHow",

nil] retain];


Yes. It will create an NSArray object and store it on the heap. The arrayWithObject method returns an autoreleased object, but your extra retain statement makes sure the reference count will be at least one, and the memory won't get freed until you explicitly release it.

It might be worth adding, it's not the "retain" statement that allocates the memory, the memory is allocated by the arrayWithObject method. The retain statement simply increments the reference count for that object.


To add to Tom's correct answer, the line:

[NSArray arrayWithObjects: ...]

is equivalent to:

[[[NSArray alloc] initWithObjects:...] autorelease]

so rather than tacking a retain onto the first line, you could just do:

[[NSArray alloc] initWithObjects:...]

In any case, the memory is allocated in alloc, whether that method appears in your code or is implicit (as it is in autoreleasing class-method convenience calls like the first one).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜