This is a little difficult to explain. I will start with factory methods. When you use a factory method such as "imageWithContentsOfFile:", it is actually doing:
m = [[[SPImage alloc] initWithContentsOfFile:@"M.png"] autorelease];
When this happens, it will show a retainCount of 1 for the current run loop/cycle. Then on the next loop, the object will be auto released. Thus the retainCount will be 0. (Note: NSLogging a retainCount of 0 will crash the app). Factory methods come in handy when you just need to add the image directly to the stage. When the image is added to an SPDisplayObjectContainer, it will be retained by that container.
Now, when you add a declare a property like:
What is really happening is:
@property (atomic, retain) m;
(Note: it is either "atomic" or "nonatomic", I can't remember which is default)
Atomic/nonatomic has to do with threading, but don't worry about that for now. Notice how the second keyword is "retain". When assigning a variable with the property (such as "self.m"), it will retain that object. So for example, it's like doing this:
m = [[SPImage imageWithContentsOfFile:@"M.png"] retain];
Which will result in a retainCount of 2 for the current run cycle, then on the next run cycle, it will be autoreleased once, then it will have a retainCount of 1.
Retaining objects is more safe to a point, since it won't cause a crash when trying to access an object that was autoreleased. But you should also be careful and make sure to release retained objects when you are done using them, like within the "dealloc" method of your class.
If you prefer to use "self.m" and retain the object, you can release it a few ways:
- Release the variable directly:
- Set the property to nil, which will autorelease the object in the setter method.
self.m = nil;
If you would rather use property methods without retaining the object at all. You can use "assign" in the keyword. Example:
@property (nonatomic, assign) m;
For more info about properties, you can read: http://www.raywenderlich.com/2712/using-properties-in-objective-c-tutorial
I haven't read this tutorial, but Ray Wenderlich has a lot of great tutorials.
I hope this makes sense, feel free to ask any more questions.