Ten minutes let you understand the Objective-C syntax (compared with Java and C ++)

Source: Internet
Author: User

Many programmers who want to develop IOS, or who are developing IOS, have done Java or C ++ before. The first time they see Objective-C code, they will have a headache, the syntax of Objective-C code is very different from that of Java and C ++. Some of them may feel like reading tianshu. However, the language is the same and has many commonalities. The following lists the comparison between the syntax of Objective-C and that of Java and C ++, so that you can easily understand the syntax of Objective-C.

First, let's see what is in the Objective-C header file and implementation file:

Header file:

Implementation file:

 

I. Comparison of functions
Helloworld Method
Java language:

Public void helloWorld (bool ishelloworld) {// What to Do}

C ++ language:

Void helloWorld (bool ishelloworld) {// What to Do}

Objective-C language:

-(Void) HelloWorld :( BOOL) ishelloworld {// What to Do}

The method with a minus sign (-) is an instance method. You must use an instance of the class to call this method. There is a plus sign, which indicates a static method of the class and can be called without instantiation.

2. Messages.
Message definition: send information to an object.
A message is a mechanism unique to the runtime environment of ios. It is similar to a class in C ++ or Java, or an instance call class or an instance method. What I'm talking about is similar. Their mechanisms are actually somewhat different.
Example:
[Object message]

[object  message:param1 withParameter:param2]NSString *string;  string = [[NSString alloc] initWithString:@"Hello"];  

The above code is similar:
Java/c ++: object. message ()
Java/c ++: object. message (param1, param2)
Java/c ++: string * str;
Str = new string ("Hello ");

Iii. Import
Example:
Import "Class. h"
Import <Class. h>
Import <director/Class. h>
This is similar to the include and java import in C ++.

Iv. Property and Synthesize

Property definition: @ property declaration is used to automatically create the getter and setter attributes of the property variable.
Synthesize definition: @ Synthesize declaration implements the getter and setter attributes of the property variable.
Example:
In interface: @ property dataType variableName
In implementation: synthesiz variableName

V. Methods in header files
Example: declare a method in the header file

-(returnType)method-(returnType)method:(dataType)param1-(returnType)method:(dataType)param1 withParam:(dataType)param2

Similar:
C/C ++/Java

returnType method()returnType method(param1)returnType method(param1,param2)

Vi. self
Pointing to your own pointer
[Self method]
 

Similar to: c ++/java
This. method ();
VII. inheritance relationship and interface implementation
Example:

ClassA:ParentAClassA:ParentA<Protocol>ClassA <Protocol>

Similar:
Java:

ClassA extends ParentAClassA extends ParentA implements interfaceClassA implements interface

The Protocol of objective-c is similar to that of c ++ and java.

8. NULL pointer

Id obj = nil;
NSString * hello = nil;

Nil is equivalent to null in Java;


9. id
Objective-c is similar to void * in 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.