10 minutes to make you understand the syntax of objective-c (vs. Java, C + +)

Source: Internet
Author: User
Tags instance method

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
    1. Public void HelloWorld (bool ishelloworld) {
    2. Do something?
    3. }

C + + language:

[CPP]View Plaincopy
    1. void HelloWorld (bool ishelloworld) {
    2. Do something?
    3. }

Objective-c language:

[CPP]View Plaincopy
    1. -(void) HelloWorld: (BOOL) ishelloworld{
    2. Do something?
    3. }

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
    1. [Object message:param1 withparameter:param2]
    2. NSString *string;
    3. 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
    1. -(ReturnType) method
    2. -(ReturnType) method: (DataType) param1
    3. -(ReturnType) method: (DataType) param1 Withparam: (dataType) param2


Similar to:
C/c++/java

[CPP]View Plaincopy
    1. ReturnType method ()
    2. ReturnType Method (param1)
    3. 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
    1. Classa:parenta
    2. Classa:parenta<protocol>
    3. ClassA <Protocol>


Similar to:
Java:

[CPP]View Plaincopy
    1. ClassA extends Parenta
    2. ClassA extends Parenta implements interface
    3. 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 + +)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.