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:

Implementation file:

A comparison of functions

HelloWorld method

Java language:

Public void HelloWorld (bool Ishelloworld)
{
do something?
}

C + + language:

void HelloWorld (bool Ishelloworld)
{
do something?
}

Objective-c language:

-(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 is actually somewhat different.

Example:

[Object message];

[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

V. Methods in the header file

Example: Declaring a method in a header file

-(returntype) method-(ReturnType) method: (dataType) param1 -(ReturnType) method: (DataType) param1 Withparam: (dataType) param2

Similar to: C/c++/java

ReturnType method ()ReturnType Method (param1)ReturnType Method (PARAM1,PARAM2)

Vi. self pointing to his own pointer

Similar to: C++/java This.method ();

Vii. inheritance relationships and interface implementations

Example:

classa:parenta classa:parenta<protocol> ClassA <Protocol>

Similar to: Java:

ClassA extends Parenta ClassA extends Parenta implements interface ClassA implements interface

OBJECTIVE-C protocol is similar to C + + and Java interfaces.

Eight, NULL pointer

ID obj = nil; NSString *hello = nil;

Nil is equivalent to NULL in Java;

Nine, id

Objective-c are similar to those in C + + (void*)

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.