A lot of programmers who want to develop iOS, or are developing iOS, have done Java or C + + before, when the first time to see OBJECTIVE-C code is a headache, objective-c code in the syntax and Java, C + + has a big difference, Some students will feel like reading the heavenly book. However, the language is interlinked, there are many similarities. The following is a list of the syntax and java,c++ of the objective-c language, so you can easily objective-c the syntax of what's going on.
Let's see what's in the Objective-c header file and the implementation file:
Header file:
The implementation file, similar to the C + +. cpp file:
A comparison of functions
HelloWorld method
Java language:
[CPP]View Plaincopy
- Public void HelloWorld (bool ishelloworld) {
- Do something?
- }
C + + language:
[CPP]View Plaincopy
- void HelloWorld (bool ishelloworld) {
- Do something?
- }
Objective-c language:
[CPP]View Plaincopy
- -(void) HelloWorld: (BOOL) ishelloworld{
- Do something?
- }
The method preceded by a minus sign (-) is an instance method that must be used by an instance of the class to invoke. Corresponding with the + number, which represents the static method of the class, does not need to be instantiated to invoke.
Second, the message.
Message definition: Sends information to an object.
Messages are a mechanism specific to the runtime environment of iOS. Similar to the C++,java class, or an instance that invokes a class or instance. What I'm saying is similar, and their mechanism actually has a big difference.
Example:
[Object Message]
[CPP]View Plaincopy
- [Object message:param1 withparameter:param2]
- NSString *string;
- string = [[NSString alloc] initwithstring:@"Hello"];
The above code is similar to the following:
Java/c++:object.message ()
Java/c++:object.message (PARAM1,PARAM2)
Java/c++:
String *str;
str = new String ("Hello");
Third, Import
Example:
Import "Class.h"
Import <Class.h>
Import <director/Class.h>
This is similar to the C + + include, Java Import
Iv. Property and synthesize
Property Definition: A getter and setter that @property declare for automatic creation of properties variables
Synthesize definition: @Synthesize declaration implements the getter and setter of the property attribute variable.
Example:
In interface: @property dataType variableName
In Implementation:synthesiz VariableName
Iv. methods in the header file
Example:
[CPP]View Plaincopy
- -(ReturnType) method
- -(ReturnType) method: (DataType) param1
- -(ReturnType) method: (DataType) param1 Withparam: (dataType) param2
Similar to:
C/c++/java
[CPP]View Plaincopy
- ReturnType method ()
- ReturnType Method (param1)
- ReturnType Method (PARAM1,PARAM2)
V. Self
Pointer to yourself
[Self Method]
Similar to: C++/java
This.method ();
Vi. inheritance relationships and interface implementations
Example:
[CPP]View Plaincopy
- Classa:parenta
- Classa:parenta<protocol>
- ClassA <Protocol>
Similar to:
Java:
[CPP]View Plaincopy
- ClassA extends Parenta
- ClassA extends Parenta implements interface
- ClassA Implements Interface
OBJECTIVE-C protocol is similar to C + + and Java interfaces.
Seven, null pointers
ID obj = nil;
NSString *hello = nil;
Nil is equivalent to NULL in Java;
Eight, ID
Objective-c are similar to those in C + + (void*)
Ps:objective-c, like Java, has a run-time environment with the ability to introspect. Objective-c and Java have a lot of different places, in the iOS system, objective-c memory needs to manage, add arc mechanism after the compiler helped objective-c add release released code. Java is managing memory through the garbage collector.
10 minutes to make you understand the syntax of objective-c (vs. Java, C + +)