Class implementation
Class implementation
Let's create an implementation, starting with the getters: Create a class implementation, starting from the accesser:
#import "Photo.h"@implementation Photo - (NSString*) caption { return caption;}- (NSString*) photographer { return photographer;}@end
This part of the code starts with @ implementation and the class name, and has @ end, just like the interface. all methods must appear between these two statements. like the class interface, the code in this section ends with @ end. The difference is that the Code starts with @ implementation and the class name. All methods must appear in the middle of the two sentences.
The getters shoshould look very familiar if you 've ever written code, so let's move on to the setters, which need a bit more explanation: If you have written code, you will be familiar with the accessors, so you should focus on setting the value method. here you need to explain more:
- (void) setCaption: (NSString*)input{ [caption autorelease]; caption = [input retain];}- (void) setPhotographer: (NSString*)input{ [photographer autorelease]; photographer = [input retain];}
Each setter deals with two variables. the first is a reference to the existing object, and the second is the new input object. in a garbage collected environment, we cocould just set the new value directly: each set-value method can process two types of variables. The first is to reference an existing object, chapter 2 creates an input object. In the environment where GC is enabled, values can be assigned directly:
- (void) setCaption: (NSString*)input { caption = input;}
But if you can't use garbage collection, you need to release the old object, and retain the new one. however, if you cannot use the GC function, you need to release the old release object and obtain the new retain object.
There are actually two ways to free a reference to an object: Release and autorelease. the standard release will remove the reference immediately. the autorelease method will release it sometime soon, but it will definitely stay around until the end of the current function (unless you add custom code to specifically change this ). there are actually two ways to release a reference to an object: Release and autorelease. The former is the standard release, and the reference is immediately removed. The automatic release of autorelease in the latter will be released as soon as possible in the near future, and will be released before the end of the method in the longest time, unless you have done something special.
The autorelease method is safer inside a setter because the variables for the new and old values cocould point to the same object. you wouldn't want to immediately release an object which you're about to retain. automatic release of autorelease is relatively secure in the value setting method, because the new and old variables can point to the same object. You cannot release the retain object immediately.
This may seem confusing right now, but it will make more sense as you progress. You don't need to understand it all yet. Now you may be confused, but it will make you better. You do not need to fully understand it now. Init Initialization Method
We can create an init method to set in1_values for our instance variables: we can create an initialization method and assign the initial values to the instance variables.
- (id) init{ if ( self = [super init] ) { [self setCaption:@"Default Caption"]; [self setPhotographer:@"Default Photographer"]; } return self;}
This is fairly self-explanatory, though the second line may look a bit unusual. this is a single equals sign, which assigns the result of [Super init] to self. this is completely self-evident, although the second line may seem a bit unusual. This is a single equal sign. Assign the result of [Super init] to self.
This essentially just asks the superclass to do its own initialization. the IF statement is verifying that the initialization was successful before trying to set default values. this statement is intended to require the class's parent class to be instantiated. The IF statement is used to determine whether the instantiation is successful and then initialize the variable. Dealloc reclaim memory
The dealloc method is called on an object when it is being removed from memory. this is usually the best time to release references to all of your child instance variables: The dealloc method is called when this object is removed from the memory. This is usually the best time to release references to child instance variables:
- (void) dealloc{ [caption release]; [photographer release]; [super dealloc];}
On the first two lines, we just send release to each of the instance variables. we don't need to use autorelease here, and the standard release is a bit faster. in the first two lines, we only send the release message to each instance variable. We don't need autorelease, because release is faster.
The last line is very important. we have to send the message [Super dealloc] To ask the superclass to do its cleanup. if we don't do this, the object will not be removed, which is a memory leak. the last line is very important. We need to send a [Super dealloc] Message to the parent class to release it. If this is not done, the object will not be removed, resulting in Memory leakage.
The dealloc method is not called on objects if garbage collection is enabled. instead, you implement the Finalize method. if the GC function is enabled, the dealloc method will not be called. In this case, you need to implement the Finalize method.
Original article: learn_objective_c Part 6
,