Enhanced knowledge of IOS (1) awareness of Objective-C

Source: Internet
Author: User

Enhanced knowledge of IOS (1) awareness of Objective-C

I have always wanted to write a general Objective-C post, but I don't have time. So I want to make the big knowledge smaller, the small knowledge smaller, and write a little bit every day, let's share what we learned with you.


1. A dynamic language OC


Object-C (hereinafter referred to as OC) is an object-oriented language, which may be known to anyone who has learned programming. However, OC is incompatible with other languages. Because it is a message structure language, rather than a function call. So it seems strange in syntax, usually like this:

[Object message];

However, one benefit of message language is its dynamic nature. The Code executed during the runtime is dynamic and determined by the runtime environment. Functional language, the code compilation stage has been determined. When compiling a function language, you need to query the function table to know the code that is running. In the message language, you do not need to know which code to run during the compilation period, because it is always found at the time of running. It does not even need to know the type of the message object, but this process is called "dynamic binding ".


The OC Runtime is different from other languages. The important work of OC is completed in the "RunTime components". All features and memory management of OC are completed in the "RunTime components, "RunTime components" are essentially dynamic libraries. Code can combine all the code written by developers. In this way, you only need to update the "RunTime components" to improve the program performance, and those functional languages need to be re-compiled.




2. "heap memory" and "stack memory"

Unlike C ++, OC does not allow allocation of the memory of OC objects to stacks and can only be allocated to heap.

C ++ string str = "123"; valid

OC NSString str = @ "123"; invalid


OC must point to an OC object with a pointer, such:

NSString * str = @ "123"; valid

We all know that the pointer memory is allocated to the stack.


For example:

NSString * str2 = str;

This is just a copy of the pointer and no new space is allocated.


The following figure is used to describe it:

The system automatically cleans up. The memory allocated to the stack is cleared by the programmer. The OC itself manages the memory in reference counting mode.


However, sometimes you will find that some variable definitions do not include "*". Then they may be allocated to the stack. (Except id, because it is already a pointer)

OC is a super set of C and fully compatible with C. Therefore, the basic types and struct can be allocated to the stack. For example:

NSInteger CGFloat CGRect CGPoint int double BOOL and so on.


The cost of creating a structure is much smaller than that of an object. If you are not familiar with the C language, you have to work hard.



3. Reference count and automatic reference count (ARC)

Reference count is a memory management policy. In short, each object requires an integer, which records the number of times the object is referenced, if a new reference (pointer copy) points to this object, the reference count is + 1. When a reference point is lost, the reference-1, if no reference points to this object, the object is destroyed.

Because OC only allows objects to be allocated to the stack, the reference count is very suitable for OC. However, the maintenance cost of the reference count is slightly higher, and it is prone to errors. The reference count is too large, resulting in Memory leakage, too small, and a wild pointer. It is difficult for programmers with less experience to achieve a balance, you need to be familiar with this mechanism and the corresponding API. Therefore, the advanced compiler supports automatic reference counting and hand over the reference counting work to the compiler. The Compiler checks and controls the reference counting, but it also has its own rules, which will be discussed in future posts.


Let's talk about this today. Thank you for watching it.


Today's highlights:

The OC is a super C, which increases the object-oriented nature. The OC uses a dynamically bound message structure. Only the runtime can know the object type and the code to be executed, which cannot be determined by the compiler.

Understand what the heap memory is and what the stack memory is.


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.