Quick reference to Objective-C syntax

Source: Internet
Author: User

DetailsObjective-CThe quick syntax reference is the content to be introduced in this article. Let's just look at the content first. Most beginners who have some development Basics on other platforms can seeXCodeFirst, I felt that I was in a bid. After seeing the Interface Builder, I felt that I was eager to try it.Objective-CThe first thought of the syntax 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.Objective-CIf you have a headache and fever, you can read this article for a quick treatment. It will take about 20 minutes, and it will never be boring.Objective-CHaving a little understanding, at least reading examples won't 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.

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:

 
 
  1. private void hello(bool ishello)  
  2. {  
  3. //OOXX  

Written in Objective-C:

 
 
  1. -(void) hello:(BOOL)ishello  
  2. {  
  3. //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:

 
 
  1. this.hello(true); 

In Objective-C, we need to write it:

 
 
  1. [self hello:YES];  
  2. 3 NS**** 

Old Joe was squeezed out of Apple and made a company called NextStep during his self-reliance portal. This entire development kit was very popular among some scientists, now Mac OS uses the NextStep function library.
 
Those who developed NextStep are more narcissistic in naming all classes in the function library with the abbreviation NextStep, that is, NS. Common examples include:

 
 
  1. NSLog  
  2. NSString  
  3. NSInteger  
  4. NSURL  
  5. NSImage  
  6. … 

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

 
 
  1. NSLog (@"%d",myInt); 

This statement is mainly used in the console for tracking. You will see the value of myInt in the console. You can see it in 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:

 
 
  1. public class Kids : System  
  2. {  
  3. private string kidName=”mykid”;  
  4. private string kidAge=“15”;  
  5. private bool isCaughtKid()  
  6. {  
  7. return true;  
  8. }  

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:

 
 
  1. @interface Kids: NSObject {  
  2. NSString *kidName;  
  3. NSString *kidAge;  
  4. }  
  5. -(BOOL) isCaughtKid:;  
  6. @end 

Write another kids. m file:

 
 
  1. #import “kids.h”  
  2. @implementation Kids  
  3.  
  4. -(void) init {  
  5. kidName=@”mykid”;  
  6. kidAge=@”15”;  
  7. }  
  8.    
  9. -(BOOL) isCaughtKid:{  
  10. return YES;  
  11. }  
  12. @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:

 
 
  1. -(void) setKids: (NSString *)myOldestKidName secondKid: (NSString *) mySecondOldestKidName thirdKid: (NSString *) myThirdOldestKidName; 

When implementing this function:

 
 
  1. -(Void) setKids: (NSString *) myOldestKidName secondKid: (NSString *) mySecondOldestKidName thirdKid: (NSString *) myThirdOldestKidName {
  2. Eldest Son = myOldestKidName;
  3. Second son = mySecondOldestKidName;
  4. Son 3 = myThirdOldestKidName;
  5. }

When calling:

 
 
  1. Kids * myKids = [[Kids alloc] init];
  2. [MyKids setKids: @ "" secondKid: @ "" thirdKid: @ ""];

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

 
 
  1. public void setKids( string myOldestKidName, string mySecondOldestKidName, string myThirdOldestKidName)  
  2. {  
  3. …  

The calling method may be as follows:

 
 
  1. Kids myKids = new Kids ();
  2. 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:

 
 
  1. [[[MyClass alloc] init:[foo bar]] autorelease]; 

The syntax for converting to C # or Java is:

 
 
  1. 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 *)

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 is NULL.

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

The @ symbol is added before, and the compiler will leave you a position in the program during compilation, so as to ensure that the string will not be lost. Remember, if you want to write some strings to the program, you need to use @ "string". If you forget to use @, the program should have an error.

Superzhou Daxia:

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

"String" is a string of C, and @ "is a shorthand for converting a string of C 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. RememberObjective-CIt's critical that C 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.

In addition, this articleObjective-CThe references are also good. If you are interested, you can read them.

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

Summary: DetailsObjective-CI hope this article will help you with the quick syntax 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.