Declare a variable
We use var to declare a variable,Congguizili like a tube placed in the experimental stage;
We give a specified variable type,Just like in the test tube labeled the;
Then put reagents must be marked on the label - otherwise could cause an explosion or poisoning。
same,If we try to give a reservoir into a wrong data type,Then the compiler will complain - yes there is always a way to be able to fool the compiler - anyway, I will not teach you this method,That would cause it to crash。
So,The tube has a capacity of,Type declaration we also have size restrictions,Will overrun error - general enough。
That is why there is a term called "overflow”。
If you are not interested in chemistry
Well,We change a chestnut:
We all know that there are some animals eat the grass,And there are some animals eat these animals graze?
Well I've got a wolf and a sheep,Wolf to eat the sheep,And the sheep to graze。
You give the wolf to sheep graze or eat wolf is wrong ..................
All right.,We still right next topic。
The value of the object is not
From now on,We know what is the reservoir,Let us talk about "open" to the reservoir where another thing - Object。
After contact with the object-oriented programming,We must face a new concept - what is the object,Of course,This is something we have discussed in the previous lesson,This lesson we focus on how to put an elephant into a refrigerator problem。
When we declare:
1 2 3 4 |
class MyClass {} let obj1 = MyClass() let obj2 = MyClass() |
Is not the instances into a memory, too? - Not really。
In fact really can not store an object storage,As we say,Nor can an elephant into household refrigerators。
Quietly tell you,In fact, the object on the "stack" on the right ......,It is garbage。
All right.,Here we have to call out another special value except nil value of this special:
Quote
In fact, that is a reference pointer。It is just a pointer to packaging - specific package became what we know - probably only Apple's siege lion can tell you。Anyway,We call this value is called reference。
Unlike C language pointer,Although reference is a pointer to an object side of the pile,But it indeed can not be directly edited。
TV and remote control
I talked about two things you may also feel that it is precisely because the old ancient ......,So I think probably everyone should be contacted。
When you generate an object,Then we say you bought a television set;Then the corresponding put your hand,Nature is the remote control it。So we say,Regardless of your generated object instance how complex,How large,Its remote control is always like that - at most, a few more buttons (method) only,The real example is actually placed on the heap。
Speaking heap
Let's talk about the correspondence between the reservoir in a reference to the object instance on the heap。
We declare a general store,And then assign it to an object,Then the store would keep a reference to,And then instantiate an object was thrown in a heap。Then,This time our remote control and TV is one to one correspondence of。
like this,We generated from two instances of the same class,Two examples was placed pile,And our hands are saved variables refer to them - like a house number。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class A { var num = 0 func doSomething() { print(num) } } var objA = A() objA.num = 9999 objA.doSomething() //9999 var objB = A() objB.num = 2222 objB.doSomething() //2222 |
What if I want to quote a copy to someone else?
1 2 3 4 5 6 |
var objC = objA objC.num = 8888 objA.doSomething() //8888 objC.doSomething() //8888 objB.doSomething() //2222 |
Yes! Another variable that will point to the same object on the heap!
In fact, now objA and objC is a reference to the same object of it!
Now I do:
1 2 3 4 5 6 7 8 |
var objC = objA objC.num = 8888 objB = objC objB.num = 4444 objA.doSomething() //4444 objC.doSomething() //4444 objB.doSomething() //4444 |
what! They have become the same object! ……and many more,That objB in the original object where to go? This not a way to never have access to it yet? ?
Yes,This time the original objB that object referenced by it alone exists in the heap - it does not matter,It has long been GC (garbage collector) gave away (do not ask me to where)。
Be chestnuts now!
In fact,Is the question on the lesson after-school friends ~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import Foundation class Echo { var count = 0 func hello() { print("hellooooo.....") } } var e1 = Echo() var e2 = e1 //把 e1 的引用赋值给 e2 //var e2 = Echo() var x = 0 while x < 4 { e1.hello() e1.count = e1.count + 1 if x == 3 { e2.count = e2.count + 1 } if x > 0 { e2.count = e2.count + e1.count } x = x + 1 } print(e2.count) |
Operating results is 24 instead of 10.
After-school title
We declare a class named HeapQuiz,This class contains only one attribute is the ID,We use it to distinguish between instances,Now we generated five instances,Then they upset references,From now on,Can you name the five reference ID corresponding instance of it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class HeapQuiz { var id = 0 } var x = 0 var hq:[HeapQuiz?] = Array<HeapQuiz?>(count: 5, repeatedValue: HeapQuiz()) while x < 3 { hq[x]?.id = x x++ } hq[3] = hq[1] hq[4] = hq[1] hq[3] = nil hq[4] = hq[0] hq[0] = hq[3] hq[3] = hq[2] hq[2] = hq[0] |
Original article written by LogStudio:R0uter's Blog » Stored value and reference
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1109.html
Comments