IPhone development-objective-C-on

Source: Internet
Author: User

Learn objective-C

Original address http://cocoadevcentral.com/d/learn_objectivec/

 

Translator's preface

I saw this article on the Internet and thought it was very good, but it seems that no one has translated it into Chinese, so I am bold enough to translate it. I have been developing software for six years, but most of them are on Windows platforms. Recently I installed a leopard on my machine, and I am a newbie in objective-C, whether the translation in this article is in place is still playing a drum. If you find that the translation is incorrect, contact me. My fuel tank is cchenhao at gmail dot com.

CC is good. J. Thank you.

 

Objective-C

Objective-C is the main programming language for Mac software development. If you understand some basic object-oriented concepts and C language, it will be helpful for you to learn objective-C. If you do not know C, you are advised to read the C guide first.

This guide is written and illustrated by Scott steven son.

 

Method call

To get started as soon as possible, let's take a look at some examples.

The basic syntax for calling an object method is as follows:

 

[Object method];

[Object methodwithinput: Input];

 

Methods can return values:

 

Output = [object methodwithoutput];

Output = [object methodwithinputandoutput: Input];

 

You can also call the class method, which is also a way to create an object. In the following example, we call the string method of the nssting class to return a new nsstring class object.

 

Id myobject = [nsstring string];

 

The ID type means that the variable myobject can be any type of object. Therefore, when you compile this code, the actual type of the Code and the method compiler it implements are unknown. In our example, the object type is obviously nsstring, so we can change the object type declaration:

 

Nsstring * mystring = [nsstring string];

 

Now, this is an nsstring type variable. If we call a method that is not supported by an nsstring type object on this object, the compiler will issue a warning.

Note: There is an asterisk (*) on the right side of the object type. In objective-C, all object variables are pointer types. The ID type has been predefined as the pointer type, so you do not need to add an asterisk.

 

Nested call

In many programming languages, nested methods or function calls look like this:

 

Function1 (function2 ());

 

The return value of function2 is passed to function1 as an input parameter. In objective-C, nested calls look like this:

 

[Nsstring stringwithformat: [prefs format];

 

Avoid nesting more than two layers in a row of statements, which will reduce the readability of the Code.

 

Multiple Input parameters

Some methods require multiple input parameters. In objective-C, a method name can be split into several segments. In the header file, the method declaration with multiple input parameters looks like this:

 

-(Bool) writetofile :( nsstring *) path

Atomically :( bool) useauxiliaryfile;

 

You can call this method as follows:

 

Bool result = [mydata writetofile: @ "/tmp/log.txt" atomically: No];

 

These are not named parameters. In the runtime environment, the method name is actuallyWritetofile: atomically:

 

Accessors

In objective-C, all instance variables are private by default. Therefore, in most cases, you should use the accessors to obtain or set the values of these variables. There are two types of syntax. The following is the traditional 1.x Syntax:

 

[Photo setcation: @ "day at the beach"];

Output = [Photo caption];

 

The first line of code does not directly read instance variables. In fact, it is calling the method named caption. In objective-C, you do not need to add a "get" prefix before the getter in most cases. In any case, the code in square brackets means that you are sending a message (a method call) to an object or a type ).

 

Point Operator

In Mac OS X 10.5, objective-C 2.0 adds the setter and getter operators of the vertex OPERATOR ):

 

Photo. Caption = @ "day at the beach ";

Output = photo. Caption;

 

You can use either of the two syntaxes, but you 'd better use only one of them in a project. At the same time, the dot syntax can only be used on Setter and getter, but not for common methods.

 

Create object

There are two main methods to create an object. The first one is what you saw earlier:

 

Nsstring * mystring = [nsstring string];

 

This is a more convenient and natural way. In this way, you create an automatic release(Autoreleased)Object. We will see more details later. However, in many places, you may need to create an object manually, as shown below:

 

Nsstring * mystring = [[nsstring alloc] init];

 

This is a nested method call. The first is the alloc method call of the nsstring class. This is a relatively low-layer call, which is used to allocate memory and instantiate an object. The second is to call the newly created objectInitMethod. The init method usually performs object initialization settings, such as creating instance variables. As a class user, you cannot know the implementation details of these methods. In some cases, you can useInitAnother method version with input parameters:

 

Nsnumber * value = [[nsnumber alloc] initwithfloat: 1.0];

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.