1, understand the origin of objective-c language

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.

Messaging (OBJECTIVE-C)

Object *obj = [Object new];

[obj Performwith:parameter1 and:parameter2];

Function Calling (C + +)

Object *obj = new Object;

Obj->perfrom (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 that is called in the sample code is polymorphic, then it is run with the virtual table (virtual table) (Ps:virtual Method table is the programming language for "Dynamic Dispatch" (Dispatch) or " A mechanism used by runtime method bindings (runtime methods binding) 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 face object attributes 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 of the work is done in the "compiler" (compile time) language, and if you want to get a similar performance boost, recompile your application code.

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:

NSString *somestring = @ "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 lightens to 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;

Error:interface type cannot be statically allocated

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:

NSString *somestring = @ "the string";

NSString *anotherstring = somestring;

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 architecture 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.

Figure 1-1 This memory layout diagram shows a NSString instance allocated in the heap, with two pointers assigned to the stack pointing to that instance

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. OBJECTIVE-C runtime Environment This part of the work is abstracted into a set of memory management architecture called "reference count".

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;

frame.origin.x = 0.0FK;

FRAME.ORIGIN.Y = 10.0f;

Frame.size.width = 100.0f;

Frame.size.height = 150.0f;

CGRect is a C struct, which is defined as:

struct CGRect {

Cgpoint origin;

Cgsize size;

};

typedef struct CGRECT CGRect;

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 start writing objective-c code, it is recommended that you take a look at the C language tutorial to familiarize yourself with its syntax. If you are not familiar with C language and go directly into objective-c, then some grammar may seem more confusing.

*************************************************************************************************************** ***

Focus:

1, Objective-c for C language has added object-oriented features, is its hyper-set. 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.

2, understand the core concept of C language to help write good Objective-c program, especially to master the memory model and pointers.

  

1, understand the origin of objective-c language

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.