Chapter One-Understanding the origins of the Objective-c language

Source: Internet
Author: User

Let's just say this: the difference between the message structure and the function call

is also a method of invoking an object,

Objective-c

Object *obj = [Object new];

[obj Performwith:parameter1 and:parameter2];

C++

Object *obj = new Object;

Obj->perform (Parameter1, parameter2);

There is not only a grammatical difference between them, but the key difference is what happens when the compiler executes this code:

    • Language of message structure:
      • Code that should be executed when run-time is determined by the runtime environment
      • Regardless of polymorphism, always at run time to query the method to be executed (how does the Objective-c compiler know which implementation to implement, to know the runtime)
    • Language to use the function call:
      • determined by the compiler.
      • Of course, if the function being called is polymorphic, the compiler will follow the "virtual method table" to find out exactly which implementation to execute, and the language of the message structure is different.

The above simply tells the difference between the language of the message structure and the language of the function call, and OBJECTIVE-C is a message-based language evolved from Smalltalk.

In the case of code written with objective-c to the actual rendered effect, the middle is actually linked by a "runtime component". In this chapter there is a phrase "update only the run-time components to improve application performance, and that much of the work is done in the" compile time "language, want to get similar performance gains, need to recompile the application code", we write the project need to go through the Apple Store audit release, each commit version is a wait, Then can we use the powerful runtime to update the project in a timely manner through the background? Of course, concrete implementation is not few words can do out, hehe.

As is known to all, objective-c can be mixed with C language, for what, because Objective-c is C's "superset", what is called "superset", meaning that all the functions of C language is still useful in writing objective-c code, Of course, sometimes you need to add the system library to the project to introduce header files and so on.

So that want to learn objective-c, use 6 words, C language is necessary to master, because objective-c system library, Apple provides us with Cocoa Foundation although encapsulated very well, but sometimes can not meet our daily development, So we need to get to the bottom of the foundation, but they are written by the C language, for the underlying foundation, others do not say, at least the syntax to be able to understand it, more in-depth, memory management to understand it, Objective-c in the C language provided by the interface generated by the object is generally not within the arc range, many times it is necessary to manage the memory of the object, so that this C still have to learn a bit. Furthermore, the underlying operating mechanism of OBJECTIVE-C, the code that we write with Objective-c is compiled by the compiler to first convert the run-time component to C, then down, and Objective-c object, class, method call, function body, etc. are implemented by the C language, so want to learn more about objective-c words, a good study of C, I also have to eat breakfast without eating on the red Book of the C What to do.

Next, simply say the heap, stack, object, variable, look at the code:

NSString *somestring = @ "Jk_chan";

NSString *anotherstring = somestring;

These two lines of code, there is only one instance object, that is through the @ "" generated nsstring instance, there are somestring,anoterstring these two variables, you can see what is the object and the variable it.

Then, the object will be generated from the memory of the application space, the memory space is on the heap, and variables, when the teacher said variables are used to store data, indeed, since to store data, then also have to have a piece of memory to do, and then the variable where the memory and the object is there is a difference, The object is just said to be on the heap, and the variable is on the stack. So what's the difference between the heap and the memory on the stack?

The memory in the heap needs to be managed directly, and the stack's memory is automatically cleaned up when the stack frame pops up, what is the stack frame popup? Should be linked to the scope of the variable, what is the scope? Call the teacher also tuition, haha, a joke.

Since the memory in the heap is the object needs to be managed directly, but it seems that we do not deliberately manage when writing code, well, in fact, there is deliberately to manage, if you know the variable ownership modifier. It's just that the memory we manage is not directly allocated, freed up to manage objects, but follows Apple's set of memory management architectures called "Reference Counting", which is arc.

Allocation of memory and how to allocate, how much? In a nutshell, the amount of memory in the heap is allocated according to the number of bytes that the object belongs to, such as the internal variables of the class, and the memory of the variables in the stack, like the variables in Objective-c, is actually a pointer, and how many bytes are allocated for the corresponding type of address.

Look back at the two lines of code, to do an extension beyond the chapters:

NSString *somestring = @ "Jk_chan1";

NSString *anotherstring = somestring;

Since two variables point to the same object, that is, to the same piece of memory, if you can change the object's memory data by invoking some of the methods in the variable, then the data referred to by the other variable will of course be different. But, like me, I've always felt, for example:

anotherstring = @ "Jk_chan2";

Well, anotherstring refers to the object changed, then why the output somestring time or jk_chan1 instead of changed jk_chan2 it, before clearly they are the two variables refers to the same object address Ah?

In fact, I am silly xxx, how to say?

anotherstring = @ "Jk_chan2";

This line of code should be spoken in Mandarin, is to generate a value of CHAN_JK NSString object assigned to the anotherstring variable, that is, the value of the variable changed, it refers to the memory address is different, it changed its heart, it fell in love with another object, In other words, the old object or the old object, it is in the memory of the data is still unchanged to the original airport or the airport, but that variable TMD, pointing to other objects to go. So the memory data referred to by somestring is still the same.

And then there is the question of reference counting.

The original Jk_chan1 Obviously there are two people like their own, somestring,anotherstring well, the heart is toward their own, but good, there is a sense of faith, point to 2, so there is a person like himself, no way ah, ╮(╯▽╰)╭, so here Jk_ The Chan1 holder is only somestirng and the reference count is 1. If the somestirng are changed, well, no one loves themselves, dead count it, this time the memory of Jk_chan1 is freed from the heap.

And then we'll mutate:

nsmutablestring *somestring = [@ "123" mutablecopy];
nsmutablestring *anoterstring = somestring;
NSLog (@ "somestring:%@,anoterstring:%@", somestring, anoterstring);
[Anoterstring appendstring:@ "456"];
NSLog (@ "somestring:%@,anoterstring:%@", somestring, anoterstring);

What is the result of the output?

2016-03-14 00:23:47.830 nice_project[1541:122585] somestring:123,anoterstring:123
2016-03-14 00:23:47.831 nice_project[1541:122585] somestring:123456,anoterstring:123456

Why is that?

Because anoterstring although this object is the airport, but still do not abandon, just let her more fill only ah, good, fill up, and then somestring also benefited, why? Because they all have a common object ah, there is no way ah, who told you directly to the object of their own to be assigned to others, right, how to do? Well done ~

Take a look at the objective-c copy:

Change the code:

nsmutablestring *somestring = [@ "123" mutablecopy];
nsmutablestring *anoterstring = [somestring copy];
NSLog (@ "somestring:%@,anoterstring:%@", somestring, anoterstring);
[Anoterstring appendstring:@ "456"];
NSLog (@ "somestring:%@,anoterstring:%@", somestring, anoterstring);

OK, compile a bit ~ ~ ~ What will output????

WTF, the direct collapse, haha, why? See just that connection, should know, copy out of the mutable type is immutable version, immutable version you also have a appendstring, (runtime brother in the method list can not find this name, and gave you 3 times to save the opportunity to cherish, do not hang your program hanging who???? Don't know what to say? Look here Runtime), and here want to not let anoterstring own object, originally should not use Copy, why? Because the copy of the object is still pointing to that address.

Change it again:

nsmutablestring *somestring = [@ "123" mutablecopy];
nsmutablestring *anoterstring = [somestring mutablecopy];
NSLog (@ "somestring:%@,anoterstring:%@", somestring, anoterstring);
[Anoterstring appendstring:@ "456"];
NSLog (@ "somestring:%@,anoterstring:%@", somestring, anoterstring);

Results?

Yes, who call you somestring do not call your object to fill, the airport or the airport, I anotherstring now have their own object, I just call my own object to fill it, right.

2016-03-14 00:37:33.761 nice_project[1597:125753] somestring:123,anoterstring:123
2016-03-14 00:37:33.762 nice_project[1597:125753] somestring:123,anoterstring:123456

Finally, to copy the main points of the book:

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. After accepting a message, what code to execute 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.

More exciting, please pay attention!

(PS: My blog is usually some of their own summary, there may be some problems in the place, you sir a lot to forgive, found that there is a problem to enjoy spraying me, haha)

Chapter One-Understanding the origins of the 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.