Objective-C syntax Quick Reference

Source: Internet
Author: User

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.

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.

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

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.  
  4. //OOXX 
  5.  

Written in Objective-C:

 
 
  1. -(void) hello:(BOOL)ishello 
  2.  
  3.  
  4. //OOXX 
  5.  

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. 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:

◆ NSLog

◆ NSString

◆ NSInteger

◆ NSURL

◆ NSImage

...

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 (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:

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

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.  
  3. NSString *kidName; 
  4.  
  5. NSString *kidAge; 
  6.  
  7.  
  8. -(BOOL) isCaughtKid:; 
  9.  
  10. @end 

Write another kids. m file:

 
 
  1. #import “kids.h” 
  2.  
  3. @implementation Kids 
  4.  
  5. -(void) init { 
  6.  
  7. kidName=@”mykid”; 
  8.  
  9. kidAge=@”15”; 
  10.  
  11.  
  12. -(BOOL) isCaughtKid:{ 
  13.  
  14. return YES; 
  15.  
  16.  
  17. @end 

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

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 .... ;

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.  
  3. Eldest Son = myOldestKidName;
  4.  
  5. Second son = mySecondOldestKidName;
  6.  
  7. Son 3 = myThirdOldestKidName;
  8.  
  9. }

When calling:

 
 
  1. Kids * myKids = [[Kids alloc] init];
  2.  
  3. [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.  
  4. … 
  5.  

The calling method may be as follows:

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

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 indicates a NULL pointer.

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.

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.

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.

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.