Objective-C syntax quick reference by cocoa China

Source: Internet
Author: User

Objective-C syntax quick reference by cocoa China

10 Apr

Most beginners who have a basic development experience on other platforms are eager to see xcode. After seeing the interface builder, they are eager to get started with the objective-C syntax, the first thought becomes daunting. Okay, I'm talking about myself.

 

If you are like me, you are interested in apple-related development: Mac OS X or iPhone, but the first time you see objective-C, it will cause headaches and fever, the quick treatment method with better curative effect is to read this article. It may take about 20 minutes, and it will never be boring. You will have a little understanding of objective-C, at least reading the example will not be a headache.

 

However, if you want to have a little c ++, C #, or Java foundation, you can at least see the source code of C ++, C #, or Java, and you can roughly understand what you are talking about.

 

This article is not a technology article. I hope you will not read it as a technology article. The article is not rigorous, but I believe you can understand it.

 

I. What are xcode, objective-C, and cocoa talking about?

Answer: There are three things.

 

Xcode: You can regard it as a development environment, just like Visual Studio, netbeans, or sharpdevelop. You can think of interface Builder as a separate program that is used to draw interfaces in Visual Studio.

 

Objective-C: This is a language, as if C ++ is a language, Java is a language, C # is a language, and the history of Yingge is also a language.

 

Cocoa: There are a lot of function libraries, such as MFC,. net, and swing. People have already written a bunch of ready-made stuff. You only need to know how to use it.

 

Some may confuse objective-C and cocoa, as if some may confuse C # And. net. These two things are actually two different things.

Ii. What is objective-C?

You can think of it as a C language with slightly different syntax. Although at first glance you may think it is a Martian language, it is different from any language you know.

 

First, let's briefly list the differences:

 

Question 1: I see a lot of minus signs, brackets, and NS ***** in the program. What are they?

 

1 minus sign (or plus sign)

 

Minus sign indicates a function, method, or message start.

 

For example, in C #, a method may be written as follows:

Private void Hello (bool ishello)

{

// Ooxx

}

 

Written in objective-C:

-(Void) Hello :( bool) ishello

{

// Ooxx

}

Is that understandable?

 

However, there is no concept of public and private in objective-C. You can think it is all public.

 

The plus sign means that other functions can directly call this function in this class without creating instances of this class.

 

2 brackets

 

Brackets can be thought of as how to call the method you just wrote. In objective-C, we usually say "message ".

 

For example, you can write in C # as follows:

 

This. Hello (true );

 

In objective-C, we need to write it:

 

[Self Hello: Yes];

 

3 NS ****

 

Old Joe was squeezed out of apple, and a company calledNEXTSTEP, which is a complete set of development kits that some scientists like, And now Mac OS usesNEXTSTEP function library.

 

These developmentNEXTSTEP people use all classes in the function library in a more narcissistic manner.NEXTSThe abbreviation of TEP is naming by the header, that isNS. Common examples include:

 

NSLog

NSString

NSInteger

NSURL

NSImage

...

 

You will often see that some of the teaching will use:

Nslog (@ "% d", Myint );

 

This statement is mainly used in the console for tracking. You will see the value of Myint in the console (open the dbg window when running in xcode ). In other development environments, we may prefer to use MessageBox for debugging.

 

You can also see other classes with names, such as CF, CA, CG, and ui, such

Cfstringtokenizer

Calayer, which indicates the layer of core Animation

Cgpoint indicates a vertex.

Uiimage: The image in the iPhone

 

Cf is about core Foundation, CA is about core animation, CG is about core graphics, and UI is about iPhone user interface ...... There are many other things you can find yourself.

 

Question 2: # What do import, @ interface, and so on?

 

1. # Import

 

You can think of it as # include. But it is best to use # import. Remember this.

 

2. @ interface and so on

 

For example, you can write a definition for the Child grabbing class in C:

 

Public class kids: System

{

Private string kidname = "mykid ";

Private string kidage = "15 ";

Private bool iscaughtkid ()

{

Return true;

}

}

 

Of course, the above statement is not necessarily correct. It is an example of a syntax.

 

In objective-C, you have to write the following:

 

First, write a kids. h file to define this class:

 

 

@ Interface kids: nsobject {

Nsstring * kidname;

Nsstring * kidage;

}

-(Bool) iscaughtkid :;

@ End

 

Write another kids. M file:

 

# Import "kids. H"

@ Implementation kids

-(Void) Init {

Kidname = @ "mykid ";

Kidage = @ "15 ";

}

 

-(Bool) iscaughtkid :{

Return yes;

}

@ End

 

This statement is not necessarily correct either. It is mainly to look at the syntax. -_-B

 

Question 3: How does one method pass multiple parameters?

 

A method can contain multiple parameters, but names must be specified for all parameters.

 

Writing multiple parameters

 

(Method data type) function name: (parameter 1 data type) value of parameter 1 Name of parameter 2 Name: (parameter 2 data type) Name of parameter 2 value .... ;

For example, the definition of a method:

 

-(Void) setkids: (nsstring *) myoldestkidname secondkid: (nsstring *) mysecondoldestkidname thirdkid: (nsstring *) mythirdoldestkidname;

 

 

When implementing this function:

 

 

-(Void) setkids: (nsstring *) myoldestkidname secondkid: (nsstring *) mysecondoldestkidname thirdkid: (nsstring *) mythirdoldestkidname {

Eldest Son = myoldestkidname;

Second son = mysecondoldestkidname;

Son 3 = mythirdoldestkidname;

}

 

When calling:

 

 

Kids * mykids = [[Kids alloc] init];

[Mykids setkids: @ "" secondkid: @ "" thirdkid: @ ""];

 

However, if you use C # To write this method, the general statement may be:

 

Public void setkids (string myoldestkidname, string mysecondoldestkidname, string mythirdoldestkidname)

{

...

}

 

The calling method may be as follows:

 

Kids mykids = New Kids ();

Mykids. setkids ("Zhang Dali", "Zhang erli", and "Zhang Xiaoli ");

 

Do you understand? In fact, it is not difficult to understand.

Basically, if you can understand the Conversion Relationship of the following code, your objective-C syntax can be roughly achieved:

 

[[Myclass alloc] init: [Foo bar] autorelease];
 

The syntax for converting to C # or Java is:

Myclass. alloc (). INIT (FOO. Bar (). autorelease ();

3. Other things

 

In fact, the previous articles on this site mentioned this. I will explain it in detail here.

 

1. ID:

 

Objective-C has a special data type: ID. You can think of it as "casual ".

 

In objective-C, everything is saved as a pointer, and you get the location of the object in the memory. So ID is the way you know the location, but you don't know what it is.

 

2. Different objects can be saved in the same array:

 

For example, an array nsarray, which can store various objects, such:

 

Myarray <-|

 

0: (float) 234.33f

1: @ "I am a good guy"

2: (nsimage *)

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.