Objective-C syntax quick reference-(Getting Started)

Source: Internet
Author: User
Objective-C syntax Quick Reference

By cocoa China[The hottest Mac, iPhone Professional Development Forum] [permanent address of this article]

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.

(Add a static member function)

 

2 brackets

 

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

(Brackets in Object C indicate method calls)

 

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 ". (Equivalent to void * in C *)

 

In objective-C, everything is saved as a pointer, and you get the location of the object in the memory. So the ID is where you knowBut I 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 *) (meitu)

3: @ "I am really a good guy"

 

This is an array composed of four things. This array includes a floating point number, two strings, and an image.

 

3. bool, yes, no:

You can think that Yes indicates true in C # or Java, and no indicates false. In fact, yes is 1, no is 0, and bool itself is a char.

 

4. What are iboutlet and ibaction.

These two things do not actually play a major role in syntax. If you want to see the control object in interface builder, add iboutlet before the definition to see the outlet of this object in IB, if you want to control an object to execute certain actions in interface builder, add (ibaction) before the method ).

 

These two things are actually the same as void.

 

5. Nil.

In objective-C, null indicates a null pointer.

 

6. Why @ "string" instead of "string"

 

 

"String" is a string of C,

@ "String" is a shorthand for converting a C string to an nsstring.
This conversion is required only when nsstring is needed, for example, in nslog.
The "string" is used where the C string is required.

In addition, @ "" This conversion does not support Chinese characters. For example, nslog (@ "string"); it must be unable to output Chinese characters.

 

Iv. Objective-C 2.0

Objective-C 2.0 is a new language of leopard. It is actually the same as the original objective-C. Attribute is added. For more information, see Allen Dang's article.

 

Http://blog.codingmylife.com /? P = 81

 

V. Summary

 

Let's summarize how to read the objective-C code and how to start learning objective-C.

 

1. Remember that objective-C is C. It is critical that it is not a Martian language.

2. Remember that you can't understand it, but it doesn't mean your mind is too dull. Most people may be even slower to read objective-C code for the first time.

3. Add cocoachina.com to your favorites. If you do not understand the code, read this article again.

4. The document is critical. When you cannot understand what something is said, check cocoachina first and then read the API description in the English document, especially when this class starts with ns. If you can't do this, search for Google and paste the method you want to check into Google. Many people are usually asking the same question. Naturally, some enthusiastic people are willing to help answer this question.

5. You can see the Hello world example, but you can't always look at it. It will be dizzy if you look too much. In addition, you must give up the official example of currency conversion in Apple's currency converter, which is a poison. The more you learn, the more you see it.

6. The best way to learn a language is to use it first. Like learning a foreign language, you will naturally read it when you speak. Set up a simple goal for yourself, such as creating a simple program and solving the problem a little bit. In this way, learning is much faster than just looking at examples.

 

This is an article written by a beginner. I hope it will help you a little bit if you are also a beginner :) although it is just a little superficial, it should be helpful for you to enter the door of objective-C. After reading this article and looking back at other articles in cocoachina, you will feel very pleasing to the eye. Remember the days.

 

In addition, this objective-c Reference is also good. If you are interested, you can read it.

Http://www.otierney.net/objective-c.html.zh-tw.big5

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.