With a step-by-step learning, learning more and more new knowledge, whether it is new or old, can not help the accumulation of time, and then familiar with the knowledge point of time will gradually forget, perhaps this is a latent inertia in people's life, it seems very familiar with things, wait until the real use of time, but not shot to come, Always indefinitely, so no matter how many knowledge points, how long, we should often take out the practice, so as not to forget. These are very important to us, some people will think that my memory is very good, write it can be remembered, but it is true, the words of good, good memory is not as bad as written, Said is those who lazy, so I hope that everyone can do more hands-on, often practice the knowledge we have learned, let him really remember in the heart!
The following is to explain what you learned today, it is the class and structure:
The class is represented by the keyword class, and the struct is represented by a struct, with a specific code attached below:
class SomeClass {} struct Length { // in Swift, you can initialize the variable var start:float = 0.0
var end:float = 0.0 // Func Callength () {PR Intln (self.end - Self.start)}}
// The default constructor init is called var length = length ()// point Syntax access element 7.03.0 Length.callength ()// constructor, only struct, class is not var3.09.9 )
The above is somewhat similar to OC, where element variables can be accessed through point syntax.
A specific example is attached below:
func callength (length:length) { - length.end)}callength (length)
This is used to calculate the distance between the points.
In addition to today we have learned the calculation type, not the direct value, but the system provides a getter method to get the value, and also provides a setter method to set the new value specific example
structRect {varOrigin =pointed ()varRect =hah ()varcenter:pointed {Get{Let CenterX= Self.origin.x + self. Rect.width/2.0Let centery= Self.origin.y + self. Rect.height/2 returnpointed (X:centerx, y:centery)}Set(newcenter) {origin.x= Newcenter.x-self. Rect.width/2ORIGIN.Y= Newcenter.y-self. Rect.height/2 } } //Use the mutating keyword to decorate: You can modify the value of a propertymutating func calcenter () {//return pointed (x:self.origin.x + self. rect.width/2.0, Y:SELF.ORIGIN.Y + self. rect.height/2.0)Self.center = pointed (x:self.origin.x + self. Rect.width/2, Y:self. Rect.height/2+self.origin.y)}}varRect = rect (origin:pointed (x: -Y: -), Rect:hah (width: -, Height: -) ) Rect.calcenter ()varRect1 =Rect () rect1.origin.x= -RECT1.ORIGIN.Y= -rect1. Rect.height= -rect1. Rect.width= -Rect1.centerrect1.center= Pointed (x: -Y: -) Rect1.origin
The mutating keyword allows you to make a property worth modifying.
The choice of class and struct:
Follow the general guidelines: when one or more of these conditions are met, consider using a struct
1. Structure for large use purposes: to encapsulate as few simple data as possible
2. Copy of the structure when it is worth copying
3. Any value type attribute stored in the structure weight will be copied, not referenced
4. Struct does not need to inherit another property or behavior of an existing type
Although today is not a lot of it, more is to review the previous study of the knowledge point, although not a Daniel it, but still hope to be able to communicate with friends, no matter what is technical, or what, all hope can be improved, so I hope everyone is not hesitate to enlighten, and finally for everyone attached to this lesson of an exercise, The topic is this: Define a student structure, name,age as a property, construct the method (initial value to Name,age), instance method (Pringstuinfo), class method (Printstusorry). Here is the detailed code:
structStudent {Static varName ="Small Flower" Static varAge =0.0 Staticfunc Printstusorry () {println ("Byebye")} func Pringstuinfo () {println ("haha, you're wrong.")}}student.name="Internet Cafes"Student.nameStudent.printStuSorry ()varStu =Student () stu.pringstuinfo ( )structStudent1 {varName:string?varAge:int?init () {Self.name="Lao Wang"Self.age= -} init (name:string, age:int) {self.name=name Self.age=Age } func Printstuinfo () {//string output: ""println"name = \ (self.name!) age = \ (self.age!)") println ("small plum = \ (self.name!) age = \ (self.age!)")} mutating func ChangeName (newname:string) {self.name! =NewName}Staticfunc Printstusorry () {println ("I'm thick, ~x.") }}varSTU2 =Student1 () stu2.changename ("Caomin") Stu2.printstuinfo () Student.printstusorry ()classTeacher {varName:string?varAge:int?init () {Self.name="Lao Li"Self.age= -} init (name:string, age:int) {self.name=name Self.age=Age } func Printteachinfo () {println ("name = \ (self.name!), age = \ (self.age!)") } classfunc Printteachsorry () {println ("wrong , wrong.") }}varSTU3 =Teacher () stu3.printteachinfo () Teacher.printteachsorry ( )
Well, today is here, because today is not a lot of learning, so do not say so much today, tomorrow we have to learn the UI part, although the previous study, but this is to use a new language to write, presumably we will know more or less, so in tomorrow's blog, Also hope that we have more suggestions, refueling!
Swift Learning and the creation of structures