what exact retain means?
I want to know what the following 开发者_C百科means. I have to release it? I did not allocate memory for it. Method is also class method. Any help?
object = [[class method] retain];
Prior version 2.0 Objective-C used a reference counter strategy to track and manage memory. From 2.0 a garbage collector can be activated, BUT, not yet available on the iPhone.
Have a look here about Objective-C reference counter strategy.
Now, 2011, it seems like the GC mechanism have been deprecated in favor for an Automatic Reference Counting (ARC) mechanism.
From Apple
Automatic Reference Counting
Automatic Reference Counting (ARC) for Objective-C makes memory management the job of the compiler. By enabling ARC with the new Apple LLVM compiler, you will never need to type retain or release again, dramatically simplifying the development process, while reducing crashes and memory leaks. The compiler has a complete understanding of your objects, and releases each object the instant it is no longer used, so apps run as fast as ever, with predictable, smooth performance.
Assuming "method" is following convention it will be returning either an autoreleased reference, or something guaranteed to be valid during the scope of the caller (unless the method is called alloc, new or copy). So without the retain the reference should be valid in the immediate call context but if you wanted to hold on to it in an instance variable you need the retain.
So, if you'll only be using "object" in the immediate context of the call you don't need the retain - otherwise you do.
If you'll be doing retain counts it's quite important you familiarise yourself with the semantics. There are many suitable references on the web, but I'll repeat the one epatel has already given, which is another stackoverflow question.
精彩评论