开发者

Do I have to release accessoryView?

UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
[switchView adTarget:selfaction:@selector(switchChanged:)forControlEvents:UIControlEventValueChanged];
[switchView release];

I al开发者_Go百科located switch view in tableView: cellForRowAtIndexPath:. Do I have to release cell.accessoryView in dealloc?

Isn't this better than above?

cell.accessoryView = [[[UISwitch alloc] initWithFrame:CGRectZero]autorelease];


You can release your switch as soon as you set cell.accessoryView. There isn't much difference between the 2 snippets, except it is generally recommended that you should release the memory explicitly wherever you can. Autorelease is generally used otherwise, such as when you have to return a value from a method.


For the purposes of this question, they both do the same thing. There's really no difference between them.

Calling autorelease can be thought of as doing the same thing as calling release in terms of memory management rules - it's actually a deferred release call. You're basically saying, "please release this object at some point in the future". Of course, in your example calling [switchView release] will only decrement the retain count, not completely release the object, since you've retained it by setting it to be the cell's accessory view.

There is a similar example that deals with the pros/cons (or lack thereof) here:

Autorelease vs. Release

You can read Apple's guidelines and documentation on autorelease pools here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html%23//apple_ref/doc/uid/20000047-CJBFBEDI

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜