objective c - Returning other objects on init -
I've read in many places, you should always start Object-Object Objects like this:
- (id) init {if (self = [super init]) {....} self-back; }
Because super init
can return a different object from the existing self
.
Now I'm trying to do something like this, and I'm not sure that I have the right, it should be seen how to maintain and release:
- (id) init: (Int) idx {id obj = [find the cache itself: idx]; If (obj) {[self release]; Self-sustaining [obj]; } And {self} [self-doLoad]; } Healthy return; }
I am mostly curious if this is the right way to maintain and issue self
and obj
. Is there a better way?
You are correct about [self init]
because partly Cocaça class returns a different object than actually allocated. However, this is an exception rather than a rule, and it is extremely rare or should not be done in its own code. Although it may be tempting to block the -init
call, you are going against the grains of the established conference and the objective-programmers expect the code to be done.
This is usually a bad method of the method, because -init
methods should be as straightforward as possible, and in fact must be related to starting the object. I would probably like to write a feature method like this:
+ Example of the FOREndex: (NSUInteger) index {id obj = [cache find itself: index]; If (obj == blue) {obj = [[All align] init]; // Add to cache} Return [[Object Written] AutoCare]; }
Then call this method instead of -init
. This will give clean code to -init
.
Besides, I'm not sure what your cache
class does, but to implement it, to reconsider and to store examples using a hidden steady variable (For example, NSMutableDictionary, where the key is an NSNumber created from an index). Can be used.
Comments
Post a Comment