The id type has been predefined as the pointer type.
Accessors
Certificate -----------------------------------------------------------------------------------------------------------------------------------
In Objective-C, all instance variables are private by default. Therefore, in most cases, you should use the accessors to obtain or set the values of these variables.
In Objective-C, you do not need to add a "get" prefix before the getter in most cases.
By default, the compiler returns an object of the id type. All input parameters are of the id type by default.
There are two methods to release an object reference:ReleaseAndAutorelease. Standard release immediately releases object references. Autorelease will not be released after a while, but the reference will actually exist until the current method ends (unless you add custom code to change it explicitly ).
It is safer to use the autorelease method in the configurator, because the new and old values of the variables to be changed may point to the same object. But you may not want to immediately release the objects you actually want to keep.
Create object
Certificate -----------------------------------------------------------------------------------------------------------------------------------
NSString * myString = [[NSString alloc] init];
This is a nested method call. The first is the alloc method call of the NSString class. This is a relatively low-layer call. It is used to allocate memory and instantiate an object.
The second is to call the newly created objectInitMethod. The init method usually performs object initialization settings, such as creating instance variables. As a class user, you cannot know the implementation details of these methods.
In some cases, you can useInitAnother version of the method with the input parameter NSNumber * value = [[NSNumber alloc] initWithFloat: 1.0];
Init
Certificate -----------------------------------------------------------------------------------------------------------------------------------
We can create an init method to set the initialization value for our instance variables:-(id) init {
}
If (self = [super init]) {
[Self setCaption: @ "Default Caption"]; [self setPhotographer: @ "Default Photographer"];
} Return self;
This Code does not need to be explained at all, although the second line looks a bit uncommon. It is a single equal sign (=), which is used[Super init]The result is assignedSelf. This actually requires the parent class to perform (parent class) initialization. The if statement is used to verify whether the parent class is initialized successfully before trying to set (the default value of this object.
Dealloc
Certificate -----------------------------------------------------------------------------------------------------------------------------------
The dealloc method is called when an object is deleted from the memory. Usually, instance variables in all objects are released in this method.
-(Void) dealloc {
; [Photographer release]; [super dealloc];
}
In the first two lines, we directly called the release method of instance variables. Here, we do not need to use autorelease because standard release is faster.
The last line is very important. We sent a [super dealloc] Message, requiring the parent class to be cleaned up. If we do not do this, the object will not be deleted from the memory, which causes memory leakage.
When the garbage collection mechanism is enabled, the object's dealloc method will not be called. In this case, you can implement a finalize method to replace it.
Memory Management
Certificate -----------------------------------------------------------------------------------------------------------------------------------
In practice, we usually create an object only for two reasons: 1. reserved as an instance variable. 2. Use the function as a temporary variable. In most cases, the setter of an instance variable is automatically released (autorelease)
The referenced object is retained at the same time. You only need to ensure that it is released (release) in the dealloc function.
In this case, we only need to locally reference the management function. There is only one rule here: if you have created an object through alloc or copy, you can send a release or autorelease message to it at the end of the function. If you create an object in other ways, do nothing.
Note that whether or not you assign values to local references as instance variables, they are all the same. You don't have to consider how setter is implemented.
If you understand this, you will understand the 90% content you need to know about Objective-C memory management.
Log records
Certificate -----------------------------------------------------------------------------------------------------------------------------------
The NSLog function calls the description method of this object and prints the NSString returned by this method to the console. You can rewrite this method in your class to return your custom string.
Properties)
Certificate -----------------------------------------------------------------------------------------------------------------------------------
Only when the accesser does not exist will @ synthesize automatically generate the accesser. Therefore, even if @ synthesize is used to declare an attribute, you can still implement custom getter and setter. The compiler automatically generates methods that you do not have.
Call methods on Nil
Certificate -----------------------------------------------------------------------------------------------------------------------------------
In Objective-C, nil Objects Act as NULL pointers in many other languages. The difference is that calling methods on nil does not cause program crashes or throws exceptions.
This technology is used in many places, but for us, the most important thing is that we do not need to check whether the object is empty before calling the method of an object. If you call a nil object method and the method returns a value, you will get a nil return value.
We can also use it to slightly improve our dealloc method:-(void) dealloc {
Self. caption = nil; self. photographer = nil; [super dealloc];
}
This can be done because when we assign the nil value to an instance variable, the setter will release the old object and keep the nil object. This method is better for dealloc, because it avoids pointing the variable to a random data, and this data happens to be another object.
Note: here we use the self. <var> syntax, which indicates that we use setter and it will perform memory management. If we only set the value directly, as shown below, memory leakage will occur:
// Incorrect. causes a memory leak. // use self. caption to go through setter caption = nil;
Category)
Certificate -----------------------------------------------------------------------------------------------------------------------------------
Unlike subclass, you cannot add instance variables by category. However, you can use existing methods in the override class. Of course, you must be especially careful when rewriting.
Remember, when you change a class through a category, this change will affect all instances of this class in your application.