Objective-c Syntax Quick Reference

Source: Internet
Author: User

Most of the other platform for beginners to develop the foundation to see Xcode, the first impression is to rub the palm of the fist, see Interface Builder, the first feeling is eager, and see Objective-c Grammar, the first impression will become deterred. Well, I'm talking about myself.

If you like me, Apple-related development: Mac OS x or iphone is interested, but the first time to see objective-c headache and accompanied by fever symptoms, the more effective rapid treatment is to read this article. It takes about 20 minutes, and it's never boring, and you're going to have a little bit of objective-c about it, at least reading an example.

But assuming that you have a little bit of C + +, C # or Java, at least you can see C + +, C # or Java source code, can roughly understand what is said.

This article is not a scientific article, I hope you do not think of it as a scientific article to read. The article is very not rigorous, but I believe you can read it.

One, XCode, objective-c, Cocoa said is a few things?

Answer: three things.

XCode: You can think of it as a development environment, like Visual Studio or NetBeans or SharpDevelop. You can think of interface Builder as a separate program for the part of the feature that is used to draw the interface in Visual Studio.

Objective-c: It's a language, just like C + + is a language, Java is a language, C # is a language, and the history of it is a language.

Cocoa: It's a bunch of libraries, like MFC,. NET, and swing, who have already written a bunch of ready-made stuff, and you just have to know how to use it.

Some people are more likely to confuse objective-c and cocoa, just as some people confuse C # and. Net. These two things are really two different things.

Second, what is Objective-c?

You can think of it as a slightly different grammatical C language. Although at first glance you may think that it is the Martian language, which is different from any language you perceive.

Let's start by simply listing a few differences:

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

1 minus sign (or plus sign)

A minus sign represents a function, or a method, or the beginning of a message, all right.

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

private void Hello (bool ishello)

{

Ooxx

}

It's written in Objective-c.

-(void) Hello: (BOOL) Ishello

{

Ooxx

}

Pretty understood, huh?

But in objective-c there is no public and private concept, you can think of all public.

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

2 Brackets

The brackets can be thought of as how to invoke the method you just wrote, usually in the objective-c to say "message".

For example, in C # you can write:

This.hello (TRUE);

In Objective-c, it is necessary to write:

[Self hello:yes];

3 ns****

Old Joe when people run out of Apple, on their own when doing a company called Nextstep, inside this set of development package is let some scientists like, and now Mac OS is Nexts Tep this set of function libraries.

The people who developed Nextstep were more narcissistic in naming all the classes in the library with the abbreviation of Nextstep, which is NS* * * *. More common examples are:

NS Log

NS String

NS Integer

NS Url

NS Image

...

You will often see some of the teachings used:

NSLog (@ "%d", myInt);

This sentence is mainly in the console inside the tracking use, you will see the console inside the value of the Myint (in Xcode when running the dbg window can be seen). In other development environments, we may be more accustomed to debugging using the MessageBox method.

You can also see some classes that begin with other names, such as CF, CA, CG, UI, and so on, such as

Cfstringtokenizer, this is a participle.

Calayer This represents the layer of core animation

Cgpoint this represents a point

UIImage This means that the pictures inside the iphone

CF says Core Foundation,ca is saying that core ANIMATION,CG is saying that core Graphics,ui is the iphone's user Interface ... There are a lot of other things that you have to discover yourself.

Question two, #import, @interface What does this kind of thing say?

1. #import

You can think of it as # include, the same. But it's best to use #import to remember this.

2, @interface, etc.

For example, you write a definition of a child-catching class in C #:

public class Kids: System

{

private string kidname= "Mykid";

private string kidage= "";

private bool iscaughtkid ()

{

return true;

}

}

Of course, the above is not necessarily the right way, is a look at the syntax for example.

You have to write this in objective-c:

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

@interface Kids:nsobject {

NSString *kidname;

NSString *kidage;

}

-(BOOL) Iscaughtkid:;

@end

Then write a kids.m file implementation:

#import "Kids.h"

@implementation Kids

-(void) init {

[Email protected] "mykid";

[Email protected] "15";

}

-(BOOL) iscaughtkid:{

return YES;

}

@end

This is not necessarily the right wording, mainly to look at the grammar on the line. -_-b

Question a city how do I pass multiple parameters?

A method can contain multiple parameters, but the following parameters are written with names.

How to do multiple parameters

(The data type of the method) function name: (parameter 1 data type) parameter 1 of the numeric value of the first name of parameter 2: (Parameter 2 data type) parameter 2 value name ...;

As an example, the definition of a method:

-(void) Setkids: (NSString *) myoldestkidname secondkid: (NSString *) mysecondoldestkidname thirdkid: (NSString *) MyThird Oldestkidname;

When implementing this function:

-(void) Setkids: (nsstring *) myoldestkidname secondkid: (nsstring *) mysecondoldestkidname thirdkid: (NSString< c2> *) mythirdoldestkidname{

The older son = myoldestkidname;

two sons = Mysecondoldestkidname;

Three sons = Mythirdoldestkidname;

}

When called:

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

[Mykids setkids: @ "Zhang vigorously" Secondkid: @ "Zhang Yili" thirdkid: @ "Zhang Xiao Li"];

And if you write this method in C #, the general wording might be

public void Setkids ( string myoldestkidname, String mysecondoldestkidname, Stringmythirdoldestkidname )

{

...

}

The approximate wording of the call might be:

Kids mykids = new Kids ();

Mykids.setkids ("Zhang vigorously", "Zhang Yili", "Zhang Xiao Li");

Do you understand? It's not really ugly.

Basically, if you can understand the conversion relationship of the following code, your OBJECTIVE-C syntax will understand 80%:

[[[[MyClass alloc] Init:[foo Bar]] autorelease];

The syntax for converting to C # or Java is:

Myclass.alloc (). Init (Foo.bar ()). Autorelease ();

Third, some of the other things

In fact, the previous articles of this site have been mentioned, here to explain in detail.

1. ID:

Objective-c has a more specific type of data is the ID. You can interpret it as "casual."

In Objective-c, everything is stored in the form of pointers, and what you get is where the object is in memory. Then the ID is the way you know the position, but don't know what it is.

2, the same array can save different objects:

For example, an array nsarray, which can hold a variety of objects, such as this array:

MyArray <--|

0: (float) 234.33f

1: @ "I'm a good man"

2: (Nsimage *) (My beauty chart)

3: @ "I'm really good"

This is an array of 4 things, including a floating-point number, two strings, and a picture.

3, Bool,yes,no:

You can say yes to indicate that true,no in C # or Java represents false. And actually yes is 1,no is 0,bool itself is a char.

4, Iboutlet, ibaction is what thing, always see.

These two things actually do not play a big role in grammar. If you want to see this control object in Interface Builder, then add Iboutlet to the definition, and you can see the outlet of the object in IB, if you want to control an object in Interface Builder to perform certain actions, Just precede the method with (ibaction).

And these two things are actually the same as void.

5, Nil.

The null (empty) in the objective-c is written so that it represents a null pointer.

6, why is @ "string" instead of "string"

Preceded by the @ symbol, the compiler will give you a location in the program when compiling, so that the string will not be lost. Anyway, remember, if you want to write some strings to die in the program, you need to use @ "string", if you forget to use @, the program should be wrong.

Superzhou:

6, why is @ "string" instead of "string"

"String" is the string of C, @ "" is a shorthand for turning C's string into NSString.
This conversion is needed where nsstring is needed, such as NSLog.
In the place where C string is required, the string is used.

In addition, the @ "" is not supported in Chinese. For example NSLog (@ "string"); It is certain that the output will not be Chinese.

Iv. OBJECTIVE-C 2.0

OBJECTIVE-C 2.0 is a new language leopard, in fact, and the original objective-c is the same. The main increase is the attribute. Details of the content is not written here, you can refer to the Allen dang This article, written very clear.

http://blog.codingmylife.com/?p=81

V. Summary

Now let's summarize how to read objective-c code and how to start learning objective-c.

1, remember Objective-c is C, not the Martian language, this is very important.

2, remember that you do not understand do not mean that the brain is dull, most people see objective-c the first time the code may be more dull than you.

3, the cocoachina.com to add to the collection, do not understand the code to see again this article at the outset of the good text.

4, the document is very important, when you do not understand something to say what is, first check Cocoachina, and then look at the API description in the English document, especially this class is the beginning of the NS. Can not go to Google search, directly to the method you want to paste into Google, usually find a lot of people are also asking the same questions, nature also has enthusiastic people live Lei Feng help answer.

5, can see Hello World example, but can't always see, see more really will faint. In addition, we must give up the official Apple currency Converter Currency Conversion example, the example is poison, just learn the more you look at the more blindfolded.

6, the best way to learn a language is to use it first, and to learn a foreign language, when you will say the nature will read. Set yourself a simple goal, such as making a simple program and then solving the problem a little bit. It's much faster to learn than to just look at examples.

This is a beginner's article, I hope you have a little help for the same beginner:) Although it is only a little bit superficial, but it should be a little help for you to enter the Objective-c door. Read this article, look back at Cocoachina other articles, you will feel very pleasing to the eye. Remember every day to come oh.

In addition, this objective-c reference is also better, if you are interested to read.

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

Objective-c Syntax Quick Reference

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.