Log records
In objective-C, it is very easy to output log information to the console. In fact, the nslog () function is similar to the printf () function in C language.% @A symbol represents an object.
Nslog (@ "the current date and time is: % @", [nsdate date]);
You can output the information of an object as a log in the console. 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.
Property (Properties)
We have previously written caption and author access methods. You may have noticed that the code is simple and can be written in a more universal form. Attribute is a feature of objective-C. It allows us to automatically generate accessors and has other benefits. We use attributes to rewrite the photo class.
The previous code looks like this:
# Import <Cocoa/cocoa. h>
@ Interface photo: nsobject {
Nsstring * Caption;
Nsstring * photographer;
}
-(Nsstring *) caption;
-(Nsstring *) photographer;
-(Void) setcaption: (nsstring *) input;
-(Void) setphotographer: (nsstring *) input;
@ End
The code after rewriting with properties looks like this:
# Import <Cocoa/cocoa. h>
@ Interface photo: nsobject {
Nsstring * Caption;
Nsstring * photographer;
}
@ Property (retain) nsstring * Caption;
@ Property (retain) nsstring * photographer;
@ End
@ PropertyIs an objective-C language instruction that declares attributes. The "retain" with parentheses indicates that the setter must retain the input value. The type and name of the specified attribute are followed by the row. Let's take a look at the implementation of this class:
# Import "photo. H"
@ Implementation photo
@ Synthesize Caption;
@ Synthesize photographer;
-(Void) dealloc
{
;
[Photographer release];
[Super dealloc];
}
@ End
@ Synthesize command generates setter and getter for us, so all we have to do is implement the dealloc method. 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 will only automatically generate methods that you do not have a custom method. Attribute Declaration has many options, but they are beyond the scope of this Guide.
Call methods on Nil
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, which manages the memory. 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 (Category)
Category is one of the most useful features in objective-C. In essence, a category allows you to add methods to an existing class without subclass or understanding the implementation details of this class. This is especially useful because you can add methods to a built-in object. To add a method to all nsstring instances in your application, you only need to add a category instead of defining a subclass to add the method. For example, if I want to add a method to nsstring to determine whether it is a URL, the statement is as follows:
# Import <Cocoa/cocoa. h>
@ Interface nsstring (utilities)
-(Bool) isurl;
@ End
This is similar to the declaration of a class. The difference is that the parent class is not listed at the end, and the name of the category is written in brackets. The name of a category can be obtained at will, but it is best to express what you want to do in the method included in the category.
The following is an implementation. Remember, this is not a good way to check the URL. We just want to clarify the concept of the category.
# Import NSString-Utilities.h"
@ Implementation nsstring (utilities)
-(Bool) isurl
{
If ([self hasprefix: @ "http: //"])
Return yes;
Else
Return no;
}
@ End
Now you can use this nsstring method. The following code prints "string1 is a URL" on the console ":
Nsstring * string1 = @ "http://pixar.com /";
Nsstring * string2 = @ "Pixar ";
If ([string1 isurl])
Nslog (@ "string1is a URL ");
If ([string2 isurl])
Nslog (@ "string2is a URL ");
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.
Summary
This article introduces basic objective-C knowledge. As you can see, this language is very easy to learn. It does not have many special syntaxes, and many standards are also used multiple times in cocoa. If you like the examples above, you can download the project and view its source code.
Learnobjectivec xcode 3.0 Project (56 K)
Http://cocoadevcentral.com/downloads/LearnObjectiveC-20080414a.zip
---------------------------------------------------- Separation line ----------------------------------------------------------
Reference/extended reading
1. Objective-C basic tutorial, by Mark Dalrymple & Scott knaster, people's post and telecommunications press, 2009;
2. The objective-C 2.0 programming language, Apple official documentation.