Recently, I have read many tutorials and daishen’s blog during my study of Swift. Finally, I put my notes here for easy review

W3cschool -Swift tutorial, YungFan’s brief book (recommended to have a look, some knowledge summary is simply easy to understand)

A, the enumeration

1, the introduction

Enum: Enumeration is a type that can have its own properties and methods, just like classes and structures

Enumeration in SWIFT, which can provide value types such as: string, character, integer value, floating point value, etc. This is different from OC, where the default integer value is

Enum CompassPoint:Int {case North case South case East case West} let STT: CompassPoint =.southCopy the code

RawValue is used to get the rawValue set by the enumerated value

Note:

  • The original value is case sensitive
  • The enumeration returned by rawValue is a selectable because the enumeration value corresponding to the original value may not exist
  • If it is an int, only the original value of the first element is specified. The original value of subsequent elements can default to +1

2, use,

Through:

// Allow traversing enum CompassPoint:Int, CaseIterable {case North case South case East case West} for method in CompassPoint. AllCases { print(method) }Copy the code

Second, the structure

1, the introduction

Struct: declaration structure, can declare member variables, member methods. So more like a class, a lightweight implementation of a class, so to speak

Note:

  • Struct instances always define your custom data types by value passing.

  • A structure is always passed through the code by being copied, so its value is immutable.

  • If you want to change a member attribute in a member method, you need the keyword mutating

2. Initialize

Struct Person {var name = "Zhangsan" var age = 10 var sex = "man" func say(){print(" Zhangsan")}} Var p1 = Person() var p2= Person(name: "Lisi", age: 20, sex: "woman")Copy the code

Code examples:

Struct Person {var name:String var age:Int func rule (){print(" I am: \(name), year \(age) ")}} // declaration: struct Person {var name:String var age:Int func rule (){print(" I am: \(name), year \(age) ")}} Var person = person (name: "xiaoMing",age: 20) person.introduce(Copy the code

3. Application scenarios

  • The main purpose of a structure is to encapsulate a small number of related simple data values.
  • It is reasonable to expect that when an instance of a structure is assigned or passed, the encapsulated data will be copied rather than referenced.
  • Any value type attributes stored in the structure will also be copied rather than referenced.
  • A structure does not need to inherit the properties or behavior of another existing type.

4. Example scenario

  • The size of the geometry encapsulates a width and height property, both of type Double.
  • A path within a range encapsulates a start attribute and a length attribute, both of type Int.
  • A point in a three-dimensional coordinate system encapsulates x, y, and z attributes, all of which are of type Double.

Operation in different cases:

Struct myStruct1 {var num1 = 100 var num2 = 200 var num3 = 300} Struct myStruct2 {var num1: Int var num2: Int var num3: Int init(mark: Int) { self.num1 = mark self.num2 = mark * 2 self.num3 = mark * 3 } }Copy the code

Call:

let stu1 = myStruct1() print(stu1.num1,stu1.num2,stu1.num3) let stu2 = myStruct2(mark: 500) print (stu2 num1, stu2 num2, stu2. Num3)/copy/value var stu3 = stu2 print (" modify before: \ (stu3. Num1) ") stu3. Num1 = 600 print (" modified: \(stu3.num1)")Copy the code

Print:

100 200 300 500 1000 1500 Before change: 500 After change: 600Copy the code

Three, class,

1, the introduction

Class: Swift uses the class keyword to define a class and accesses its properties and methods through. (dot syntax)

2, use,

Definition:

Class Person {var name = "Zhangsan" var age = 10 func say(){print(" name ")}} var p1 = Person() p1.say()Copy the code

Note: Classes do not automatically implement constructors by default. If a member variable is declared without an assignment, you need to implement constructors to perform the assignment, otherwise it will not compile

class Person { var name : String var age : Int init(name : String, age : Person(name: "zhangsan", age: 10) {self.name = name self.age = age}}Copy the code

Four,structandclassTo compare

Struct is a structure, class is a class

  • Struct is a value type, class is a reference type; Declare a new variable to refer to the structure, change a property, the original structure property does not change; And the class will change

Code examples:

Struct myStru {var width = 200} class myClass {var width = 200} var stu = myStru() var stu2 = stu stu2.width = 300 print(stu.width,stu2.width) //200 300 var cls = myClass() var cls2 = cls cls2.width = 300 print(cls.width,cls2.width) //300 300Copy the code
  • Struct cannot be inherited, class can be inherited

  • 3, Use let to modify the class object, can modify the class value; To modify the struct value, use var

  • Structs are on the stack and classes are on the heap, so structs are faster to access

  • Struct if you want to declare a func to modify the declared attribute, you need to add the keyword mutating, to compile successfully; Class does not

  • 6. All structs have an automatically generated member constructor, whereas classes need to be generated themselves

  • 7. In structs, member variables can have no initial value; But in class, if a member variable has no initial value, you must write a constructor for it