Class class in Swift and struct struct in Swift

  • 2020-05-17 06:39:04
  • OfStack

1. The introduction
Classes in Swift are similar to struct 10, but unlike Objective-C, structs in Swift can not only define properties, but also define methods for them, just like class 1.

The class and struct in Swift have the following similarities:

1. Define properties to store values.

2. Define functions to provide functionality.

3. Use subscript to evaluate by defining subscript syntax.

4. Define a constructor to initialize it.

5. Extend to add functionality to the original.

6. Define implementation standards through protocols.

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

Create subclasses of classes by inheritance.

2. Allows type checking and interpretation of instances of classes at runtime.

3. Destructor method can release the resources referenced by the class.

4. Allow multiple references to one class instance by reference counting.

When developers pass these instances in code, the structs are always assigned and the classes are referenced. This is the essential difference between a struct and a class.

2. Definition of classes and structs

Class and struct are similar in terms of definition syntax. The sample 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 class 
var obj1 = MyClass()
// Create an instance of the structure   All structures will be generated by default 1 A constructor that sets properties one by one   The class will not 
var obj2 = MyStruct(param1: 1,param2: "1")
// You can get the value of an attribute in a class or struct by clicking on the syntax 
print(obj1.age,obj2.param1)

By passing between examples, it can be proved that the class in Swift is copied to the structure. The example is as follows:


// Pass the class instance to another 1 A variable 
var obj3 = obj1
// Pass a struct instance to another 1 A variable 
var obj4 = obj2
// Change the face value 
obj3.name = "NewHS"
obj4.param1 = 2
// will   print  NewHS 1 // Note that the class is referenced   The structure is assigned a value 
print(obj1.name,obj2.param1)

Note: enumeration types are also replicated when instances are passed.

Since classes are passed by reference, Swift also provides an operator to compare whether two instance variables or constants refer to the same reference. An example is as follows:


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

Actually, === = and! The == operator compares the contents of the pointer.

3. Selection of class and structure

Since classes and structs have different delivery mechanisms, they are also suitable for different development tasks. It is recommended that developers use structs to create data types in the following situations:

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

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

3. Data types defined in this type should also be assigned values when passed.

4. There is no need to integrate another data type.

In all but one of the cases listed above, it is recommended that developers use classes to describe data as a last resort in development.


Related articles: