iOS development Learn about the origins of Objective-c languages

Source: Internet
Author: User

Objective-c is similar to object-oriented languages such as C + +, Java, but in many ways different. If you use another object-oriented language, you can understand many of the paradigms and templates used by objective-c. However, the syntax may seem strange because the language uses the message structure (messaging structure) instead of the function calling. The Objective-c language evolved from Smalltalk, which was the originator of the message-based language. The difference between a message and a function call looks like this:

 1  //  2  Object *obj = [object new  ];  3  [obj Performwith:parameter1 and:  PARAMETER2];  4  5  //  Function Calling (C + +)  6  Object *obj = new   Object;  7  obj->perform (Parameter1, parameter2); 

The key difference is that, using the language of the message structure, the code that the runtime should execute is determined by the runtime environment, and the language used by the function call is determined by the compiler. If the function called in the sample code is polymorphic, then at runtime it is necessary to follow the virtual table to find out which function implementation should be performed. The language of the message structure, whether polymorphic or not, is always run to find the method to be executed. In fact, the compiler doesn't even care what type of object the message is received from. An object problem that receives a message is also handled at run time, and its process is called dynamic binding

Objective-c's important work is done by the runtime component (runtime component) rather than the compiler. All the data structures and functions required to use Objective-c's object-oriented features are in the run-time component. For example, the run-time component contains all the memory management methods. The run-time component is essentially a "dynamic library" that is linked to a developer's code, and its code can glue all the programs written by the developer. In this case, you can improve application performance by simply updating the run-time components. And that much work is done in the "compile-time" language, compile the application code if you want to get a similar performance boost.

Objective-c is a "superset" of C (superset), so all the features in C are still applicable when writing objective-c code. Therefore, it is necessary to grasp the core concepts of both C and objective-c in order to write efficient objective-c code. It is particularly important to understand the memory model of the C language, which helps to understand how Objective-c's memory model and its "reference counting" (reference counting) mechanism works. To understand the memory model, you need to understand that pointers in Objective-c languages are used to indicate objects. To declare a variable so that it refers to an object, use the following syntax:

@" The string "

This syntax is basically a copy of the C language, it declares a variable named somestring, whose type is nsstring*. In other words, this variable is a pointer to NSString. All objects of the Objective-c language must be declared this way, because the object's memory is always allocated in heap space, and is never allocated on the stack. The Objective-c object cannot be allocated in the stack:

nsstring stackstring;   //

The somestring variable points to a block of memory allocated to the heap, which contains a NSString object. That is, if you create another variable that points to the same address, then the object is not copied, except that the two variables point to the object at the same time:

@" The string " ;  

There is only one nsstring instance, but there are two variables pointing to this instance. The two variables are nsstring* type, which indicates that the current "stack frame" is allocated two blocks of memory, each chunk of memory can accommodate a pointer (4 bytes on a 32-bit computer, 64 bytes on a 8-bit computer). Both of these memory values are the same, which is the memory address of the NSString instance.

Figure 1-1 depicts the memory layout at this point. The data stored in the NSString instance contains bytes that represent the actual contents of the string.

The memory allocated in the heap must be managed directly, and the memory allocated on the stack to hold the variable is automatically cleaned up when its stack frame pops up.

Objective-c abstraction of heap memory management. malloc and free are not required to allocate or dispose of the memory occupied by the object. The OBJECTIVE-C Runtime Environment abstracts This part of the work into a memory management architecture called reference counting (see article 29th).

In the OBJECTIVE-C code, you can sometimes encounter variables that do not contain * in the definition, and they may use "stack space". These variables are not saved by the Objective-c object. For example, CGRect in the Coregraphics framework is an example:

cgrect frame;   0.0f ;   10.0f ;   100.0f ;   150.0f

CGRect is a C struct, which is defined as:

struct CGRect {    cgpoint origin;    Cgsize size;  };   struct

This structure is used throughout the system framework, because performance is affected if you use the Objective-c object instead. Creating objects also requires additional overhead, such as allocating and freeing heap memory, compared to creating structs. If you only need to save "non-object Types" (nonobject type), such as int, float, double, char, and so on, it is usually possible to use the cgrect structure.

Before you begin to write objective-c code, it is recommended that the reader first look at the C language tutorial to familiarize itself with its syntax. If you are not familiar with C language directly into objective-c, then some grammar may confuse you.

Points

Objective-c adds an object-oriented feature to the C language and is a superset of it. Objective-c uses a dynamically bound message structure, which means that the object type is checked at run time. What code should be executed after receiving a message is determined by the run-time environment, not the compiler.

Understanding the core concepts of C language helps to write good objective-c programs. In particular, you should master the memory model and pointers.

iOS development Learn about the origins of Objective-c languages

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.