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. 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 Article It 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 separate the functions that interface builder considers to be used to draw interfaces in Visual Studio. Program . 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 slightly different syntax. C Language . 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 private void Hello (bool ishello) {// ooxx}. In objective-C, it is-(void) Hello :( bool) ishello {// ooxx} is it easy to understand? 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 can be used to call the method you just wrote. In objective-C, we usually say "message ". For example, you can write this in C #: This. hello (true); in objective-C, it should be written as: [self Hello: Yes]; 3 NS ***** Old Joe was squeezed out of apple in the past, A company called nextstep was built in the Self-Reliance portal. This entire development kit is very popular with some scientists. Now Mac OS uses nextstep as a function library. Those who developed nextstep are more narcissistic in naming all classes in the function library with the abbreviation nextstep, that is, NS. For example, nslognsstringnsintegernsurlnsimage... You will often see nslog (@ "% d", Myint) used in some teaching courses. This sentence is mainly used in the console for tracking and usage, 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, for example, cfstringtokenizer is a word splitting task called calayer. This indicates the cgpoint layer of core animation. This indicates a point of uiimage. This indicates that the image CF in the iPhone is about core Foundation, and the CA is about core animation, CG is about core graphics, and UI is about the 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. For example, you can write a definition for the child-catching class in C #: public class kids: system {private string kidname = "mykid "; private string kidage = "15"; private bool iscaughtkid () {return true ;}} of course, the preceding statement is not necessarily correct. It is an example of the syntax. In objective-C, write this statement: first write a kids. the H file defines this class: @ interface kids: nsobject {nsstring * kidname; nsstring * kidage;}-(bool) iscaughtkid:; @ end write another kids. M file implementation: # import "kids. h "@ implementation kids-(void) Init {kidname = @" mykid "; kidage = @" 15 ";}- (bool) iscaughtkid: {return yes ;} the @ end method is not necessarily correct. It is mainly used 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 Value Name of parameter 2 Name: (parameter 2 data type) name of parameter 2 value .... For example, define 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 *) Principal {eldest son = myoldestkidname; second son = mysecondoldestkidname; third son = mythirdoldestkidname;} when calling: Kids * mykids = [[kids Alloc] init]; [mykids setkids: @ "" secondkid: @ "" thirdkid: @ ""]; if you use C # To write this method, the approximate format may be public void setkids (string myoldestkidname, string mysecondoldestkidname, string mythirdoldestkidname ){...} The calling method may be: 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 following section Code The objective-C syntax can be roughly the same: [[myclass alloc] init: [Foo bar] autorelease]; the syntax for converting to C # or Java is myclass. alloc (). init (FOO. bar ()). autorelease (); 3. Some other things are mentioned in previous articles on this site. I will explain them 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 save various objects. For example, in this array, myarray <-- | 0: (float) 234.33f 1: @ "I am a good guy" 2: (nsimage *) (My 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 represents true in C # or Java, and no represents 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 is the @ symbol added before "string" instead of "string"? The compiler will leave you a place in the program during compilation so 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 hero pointed out: 6. Why is it @ "string" instead of "string"? The string "is a C string, @" is a shorthand for converting a C string into 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"); cannot output Chinese characters. iv. Objective-C 2.0objective-C 2.0 is a newly added language of leopard. It is actually the same as the original objective-C. Attribute is added. For more information, see Allen Dang's article.
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.