SoFunction
Updated on 2025-03-10

Class class and structure struct learning notes in Swift

1. Introduction
Classes in Swift are very similar to structures. Different from Objective-C, structures in Swift can not only define attributes, but also define methods for them like classes.

Classes and structures in Swift have the following similarities:

1. Define attributes to store values.

2. Define functions to provide functions.

3. Use the subscript to get the value by defining the subscript grammar.

4. Define the constructor to initialize it.

5. Add functions on the original basis through extensions.

6. Define implementation standards through protocols.

Of course, there are many differences between classes and structures. The following functions are unique to classes, and structures do not:

1. Create a subclass of a class through inheritance.

2. Allow type checks and interpretations of class instances at runtime.

3. The destructor method can release the resources referenced by the class.

4. Allow multiple references of a class instance by reference counting.

When the developer passes these instances in the code, the structure is always assigned and the class is referenced. This is the most essential difference between a structure and a class.

2. Definition of classes and structures

Classes and structures are similar in definition syntax, and the example code is as follows:

class MyClass {
  var name = "HS"
  var age = 25
}
struct MyStruct {
  var param1:Int
  var param2:String
}
//Create an instance of the classvar obj1 = MyClass()
//Create an instance of a structure. All structures will generate a constructor that sets attributes one by one by one by one by the default, and the class will notvar obj2 = MyStruct(param1: 1,param2: "1")
//The attribute value in a class or structure can be obtained through point syntaxprint(,obj2.param1)

Through the passing between examples, it can be proved that classes in Swift are referenced for structures to be copied. The examples are as follows:

//Pass class instance to another variablevar obj3 = obj1
//Pass the structure instance to another variablevar obj4 = obj2
//Clean change the value of the face = "NewHS"
obj4.param1 = 2
//Print NewHS 1 //Show the class is a referenced structure and the value is assignedprint(,obj2.param1)

Note: Enumeration types are also used in the copy principle when passing instances.

Since classes are passed through references, Swift also provides an operator to compare whether two instance variables or constants point to the same reference. The example is as follows:

if obj1===obj3{
  print("same refer")
}else if obj1 !== obj3 {
  print("not same refer")
}

In fact, the === and the !== operator are compared with the pointer content.

III. Selection of Classes and Structures

Since classes and structures have different delivery mechanisms, they are also suitable for different development tasks. In the following cases, the official recommends that developers use structures to create data types:

1. This data type encapsulates a small amount of simple data values.

2. When this type of data is passed, it should be copied.

3. The data types defined in this type should also be assigned values ​​when passed.

4. It does not need to be integrated by another data type.

In addition to some of the situations listed above, in other cases, developers are recommended to use classes to describe data, which is also the last commonly used method in development.