1. Code file type:
(1) [. H] header file
(2) [. m] C code
(3) [. mm] C ++ code
2. Call the method # import of the header file, which is called only once when it has not been called, similar to PhP's require_once.
3. String nsstring
The @ flag can be used to define an nsstring object by a String constant. For example,
Nsstring * f = @ "long years ";
4. Class)
(1) Definition
The objective-c Definition class first defines the interface in the. h header file and then implements it in the. m code file.
. H header file
The part between @ implementation and @ end is regarded as the definition of the class.
. M file
(2) Class instantiation
Class name * class instance name = [[class name alloc] init]
Or
Class name * class instance name = [class name New]
(3) Type
The objective-C object definition supports the definition of strong instances and weak instances (represented by "ID.
(4) method (methods)
Method methods is also called a function. In objective-C, classes are similar to Java classes. You can define "instance methods" and "class methods ".
Indicates the method using the negative sign [-] as an instance.
(5) send messages
The objective-C call method is to send a message to the corresponding instance object. Messages are sent in the same way as other languages, the unique feature of Ojbective-C is that a method is called through a method name + zero or multiple identifiers + zero or multiple parameters, generally, a language only requires one method name + zero or multiple parameters. The reason why Ojbective-C calls a method to send a message is that all of its messages are dynamically transmitted to the instance object after being sent. In addition, if a subclass defines a method with the same method name + identifier as the parent class, the subclass will first receive the message and then selectively pass the message to the parent class.
Messages are sent through a pair of square brackets [], as shown in figure
The instance object is on the left, and the message and parameter are defined on the right. To avoid generating unnecessary temporary variables, objective-C allows you to directly use the message result as follows:
(6) class methods
Similar to static methods, the difference between the definition and the instance method is that the symbol at the beginning is the plus sign [+].
(7), attribute @ Property
Attribute is a convenient symbol that can replace methods. When defining attributes in a class, you do not need to create a new instance. Instead, you can easily and quickly access existing variables. In other words, attributes do not actually store any data.
The read-only attribute can also define some access restrictions or how to obtain the attribute. For example, you can specify copy to copy the attribute and readonly to specify read-only for this attribute.
There are two methods to call the attributes of an instance. One is through square brackets, which is similar to the call method, and the other is through the dot symbol [.].
(8) method Return Value
In objective-C, the return type of the method needs to be enclosed in parentheses. When the compiler sees the parentheses after the minus sign or the plus sign, it will think that this is the return value of the declared method. If the return value is not declared, the compilation adds a default return value to the function that does not write the explicit return value. Its type is ID.
(Void) declares that no return value is returned.
(9), self, and super
Self is equivalent to this, and itself is a pointer variable of the ID type.
Access variable self-> variable name.
The super method or variable used to access the superclass.
5. Protocols and Delegation)
Protocols in objective-C is similar to common interfaces and is implemented in classes as defined in protocols.
6. memory usage
(1) referencecounted, that is, application count. Process: alloc ---> retain ---> release ---> dealloc.
(2), nsautoreleasepoolalloc
7. Print the nslog log
Nslog (@ "long years, thin clothes .");
8. Boolean Value
In objective-C, bool is actually singed char, yes is 1, no is 0. All the logical judgments in the logical judgments are compatible with the C language. If the data is not 0, the values are true. If the data is 0, the values are false.
9. Sel, class, IMP
(1) During compilation, objective-C will generate a unique identifier for distinguishing this method based on the method name (including the parameter sequence). This identifier is the SEL type.
Sel variable name [email protected] (method name );
Sel variable name = nsselectorfromstring (method name string );
Nsstring * variable name = nsstringfromselector (SEL variable name); // return the method name corresponding to the SEL variable.
(2) the class is similar to the class in Java and can be used to obtain information about the defined class ..
Class variable name = [class or object class];
Class variable name = [class or object superclass];
Class variable name = nsclassfromstring (Class Name string );
Nsstring * variable name = nsstringfromclass (class variable name); // return class name.
(3) imp, give a name to a method for dynamic calling, no longer using the [object message] method.
Human * Human = [Human new]; man * ma = [Man new]; // locate the ID of the method based on the method name say and bind sel with it; sel sel [email protected] (say); // You can also write sel = nsselectorfromstring (@ "say"); imp imp1 = [Human methodforselector: sel]; IMP imp2 = [Ma methodforselector: sel]; imp1 (human, Sel); imp2 (MA, Sel );
// Because each method has its own address, this method directly finds the method for distinguishing addresses with the same ID, which is the most efficient, but less flexible than the SEL method.
10. instance variable scope
@ Private
@ Protected
@ Public
It is equivalent to the corresponding Java keywords.
In objective-C, class methods are theoretically public.
11. Final
If you do not want a method to be overwritten, you only need to define a macro.
# Define final
Objective C Study Notes