Objective-C notes

Source: Internet
Author: User

1. Dynamic Allocation

Dynamic Allocation is handled by two functions: malloc and free. Use a parameter to call malloc to request memory. This function specifies the number of bytes required. Malloc returns a pointer to the number of bytes in the request. Then, the pointer can be forcibly converted to the desired data type and assigned to a variable, the type of this variable is a pointer to the requested type. Previously, when no available memory allocation is available, malloc returns NULL. However, when OS X and iOS use latency allocation, malloc returns a pointer to the requested memory. However, before executing a piece of code to access the memory, the system will not allocate memory resources for the request. As a result, if you request the memory multiple times and do not use the memory or release the memory, malloc may return a non-null value, even if there is no more memory. In this case, using the returned pointer may cause the program to crash. If you specify some typical values for Ram and swap space, even if possible, you will rarely see this happen on OS X. In iOS, before this happens, the application may receive a low memory warning from the system.

2. compiler commands

Words Starting with @ are compiler instructions rather than executable code. As we know, @ interface indicates the beginning of the Interface part defined by a class; @ implementation indicates the start of the implementation part; and @ end indicates the end of these parts. Objective-C uses the constant instance of nsstring instead of the normal C string, and nsstring is a class defined in the foundation framework. You can create an nnstring directly by creating a C string, but add a @ at the beginning of the string, for example:

"The Big Apple" // literal C string

@ "The Big Apple" // literal nsstring

Strictly speaking, @ "The Big Apple" is a Compilation instruction, which tells the compiler to create an nsstring directly, and its text is "The Big Apple ", if you use a format string with a % @ descriptor but forget to provide a corresponding object parameter, nslog will try to send a description message to the byte at the address where the object parameter should be placed. This usually causes program crashes.

3. Messages

The objective-C object is required to execute its own method, which is not the same as calling a function. Function calls are usually statically bound, that is, what happens during the runtime has been determined during compilation. When the compiler encounters a function call, it inserts an instruction to jump to the code that constitutes the function body. Objective-C uses a more dynamic system called message. When you want the objective-C object to execute its own method, a message is sent to the object. The object is implemented by executing the corresponding method of the message.

The message expression is as follows:

[Receive message]

When executing a message expression, the objective-C Runtime checks the receiver and determines its class. The runtime has a table for each class, lists the methods defined by this class, and associates each method with a pointer to the Code pointing to this method. Obtain the method name from the message at runtime, find the corresponding pointer in the receiver's class table, and then use this pointer to execute the code of the method.

How does objective-C work in the message system?

The details are as follows: 1. All objective-C objects know their own classes. To complete this task, the compiler adds an instance variable ISA to each object instance, which indicates the type of objects in an instance. At runtime, ISA points to a class structure of the class, which is an opaque type, including information about the class. Includes the instance variables and methods defined by this class, And a pointer pointing to information about its superclass. This information is filled at runtime when the program is started. 2. the compiler converts the method to the actually compiled C function. In this process, it adds two additional parameters, self and _ cmd, at the beginning of the parameter list. Self is a pointer pointing to the image of the receiver when the message is sent; _ CMD is the follower corresponding to the Method Name of the message.

Consider the following objective-C method:-(void) dosomething :( INT) anarg {....}

During compilation, the compiler converts dosomething to a C function with the following parameter list: (ID self, Sel _ cmd, int anarg)

Each objective-C class has a table that links the selector of a method with a pointer to a function that implements the method. Given the optimizer, you can use this table to find the pointer to the matched function at runtime. Each class also has a related caching mechanism, which accelerates subsequent searches for a given method.

4. What runtime?

What is the actual running time? It is not mysterious at all. The runtime is only a shared library of common C functions and structures. When the program starts to run, some such functions will be called. These functions construct tables based on the information that the compiler places in your executable code. Other functions are called to find and operate the information in these tables at different time points of the program execution. These function calls are automatically created by the compiler.

5. send messages to nil

According to the objective-C design, sending a message to an Nil receiver has no effect. When calculating an expression with a nil receiver, there is no way to execute it. The program silently ignores the NIL receiver rather than causing an exception or crash, which allows us to write clean code. You don't have to mess up the structure of the Code as follows to avoid errors:

Shape * ashape =...

If (ashape! = Nil) {[ashape draw];}

Instead, you can directly write it as: [ashape draw];

If ashape happens to be nil, nothing will be drawn and the execution starts from the next line of code. If the return value type of a method in a message expression is a pointer or number, the return value of a message sent to nil is 0. For other return types (such as structures), the return value is uncertain in some cases. In some cases, an Nil receiver may be used to raise an exception. However, in most cases, avoiding the use of the IF statement will make the code more neat. If it is important to capture an Nil receiver in a specific situation, you can test it as well.

6. Selector

The method name part of a message is sometimes called a selector or method optimizer, because it is used to select the method to be executed during running. Selector is just a name, they do not contain any type of information. Objective-C defines a type, Sel, to save the expression of the selector. Sel has a one-to-one relationship with the selector name. However, a sel itself is not a string. All selectors with the same name have the same Sel, and those with different names always correspond to different SEL. Internally, objective-C uses the SEL type to represent methods to improve efficiency. Using a string will be slow, because only testing whether the two strings are the same requires traversing all the characters in the string.

7. methods with the same name

If dynamic type is used (the variable type is ID), all methods with the same name should have the same parameter type and return value, even if they are declared by irrelevant types. If you violate this rule, you will receive a warning from the compiler; if you ignore this warning, your program may have very minor bugs.

 

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.