Objective-C learning for iOS Application Development (1)

Source: Internet
Author: User

IOSApplication DevelopmentObjective-CLearning is what we will introduce in this article,Objective-CIt is the main language for writing Mac software. If you adapt to basic object-oriented and C language, it will show you a lot of such content. If you do not know C, read the C Guide [English] first.

This guide is written and typeset by Scott steven son.

1. Calling Methods

To get started as soon as possible, let's take a look at some simple examples. Some basic syntaxes for calling methods of an object are as follows:

 
 
  1. [object method];  
  2. [object methodWithInput:input]; 

Method returns a value:

 
 
  1. output = [object methodWithOutput];  
  2. output = [object methodWithInputAndOutput:input]; 

You can also call the class method to create an object. In the following example, the string method of the NSString class is called and a new NSString object is returned:

 
 
  1. id myObject = [NSString string]; 

Type id indicates that the myObject variable can be referenced by any type of object. Therefore, during compilation, it does not know the classes and methods actually implemented.

In this example, the object type is obviously NSString, so the type can be changed:

 
 
  1. NSString* myString = [NSString string]; 

Now this is an NSString variable, so that the compiler will warn when it calls methods not supported by NSString in common sense.

Note that there is an asterisk on the right of the object type. All Objective-C object variables are pointer types. The id type is predefined as the pointer type, so asterisks are not required.

Nested messages

In many languages, nested methods or function calls are in the following form:

 
 
  1. function1 ( function2() ); 

The result of function 2 is used as the input of function 1. In Objective-C, nested messages are in the following format:

 
 
  1. [NSString stringWithFormat:[prefs format]]; 

Avoid nesting more than two calls in the migration, so that the code is more understandable.

Multiple Input Methods

Some methods accept multiple input values. In Objective-C, a method name can be divided into multiple segments. The multi-input method is as follows:

 
 
  1. -(BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile; 

You can call this method as follows:

 
 
  1. BOOL result = [myData writeToFile:@"/tmp/log.txt" atomically:NO]; 

This is not just a name parameter. In the runtime system, the method name is actually writeToFile: atomically :.

2. accessors

In Objective-C, all instance variables are private by default. In most cases, the accessors should be used to obtain or set values. There are two types of syntax. This is the traditional 1.x Syntax:

 
 
  1. [photo setCaption:@"Day at the Beach"];  
  2. output = [photo caption]; 

The second part of the Code does not directly access instance variables. It actually calls the caption method. In most cases, in Objective-C, you do not need to add the "get" prefix to getter.

When you see the code in square brackets, it means to send messages to objects or classes.

Point syntax

In Objective-C 10.5 of Mac OS X 2.0, point syntax supports getter and setter:

 
 
  1. photo.caption = @"Day at the Beach";  
  2. output = photo.caption; 

You can use either of the two syntaxes, but you can only use one of them in each project. The dot syntax can only be used in setter and getter, and cannot be used in methods for other purposes.

3. Create an object

You can create an object in two ways. The following is the first type:

 
 
  1. NSString* myString = [NSString string]; 

This is a more convenient form of automatic creation. In this example, an automatically released object is created. We will learn more details later. In many cases, you need to create an object in manual mode:

 
 
  1. NSString* myString = [[NSString alloc] init]; 

This is a nested method call. First, NSString itself calls the alloc method. This is a relatively low-level call for allocating memory and instantiating objects.

The second part is to call the init of the new object. Init usually implements some basic settings, such as creating instance variables. For users who use classes, the implementation details are unknown.

In some cases, you can use different init versions with inputs:

 
 
  1. NSNumber* value = [[NSNumber alloc] initWithFloat:1.0]; 

4. Basic Memory Management

When writing a Mac OS X program, you can enable garbage collection. In general, this means that you do not need to focus on memory management before you encounter a very complex situation.

However, it is impossible to always work in an environment that supports garbage collection. In this case, you need to know some key points.

If you create an object in the form of alloc manually, you need to release the object after use. You should not manually release any automatically released objects. If you do so, the application will exit unexpectedly.

There are two examples:

 
 
  1. // String1 will be automatically released
  2. NSString * string1 = [NSString string];
  3. // After use, the must release this when done must be released
  4. NSString * string2 = [[NSString alloc] init];
  5. [String2 release];

In this guide, we can think that automatic objects will always be released after the current function is complete.

There are still many things to learn about memory management, but we need to wait until we know more about some other things.


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.