The structure of the body

The keyword struct

Struct Date {var year: Int var month: Int var day: Int} Var date = date (year: 2019, month: 06, day: 29)Copy the code

In the Swift standard library, common types such as Bool, Int, Double, String, Array, Dictionary, and so on are structures.

Applicable scenario

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.

  • The initializer

All structures have an initializer automatically generated by the compiler, including initializer methods, constructors, and constructors. You can think of it as an init method.

If you set optional variables to a property, multiple initializers are automatically generated.

struct Point { var x: Int? var y: Int? } // create. var p1 = Point(x: 10, y: 10) var p2 = Point(y: 10) var p3 = Point(x: 10) var p4 = Point()Copy the code

class

The keyword class

Class Point {var x: Int = 0 var y: Int = 0}Copy the code
  • Class initializer

Class definitions are similar to structs, but the compiler does not automatically generate initializers for classes!!

If all members of a class are initialized at definition time, the compiler generates an initializer with no arguments for the class.

Structure and class contrast

struct Resolution {
    var width = 0
    var height = 0
}
class VideoMode {
    var resolution = Resolution()

    var interlaced = false
    var frameRate = 0.0
    var name: String?
}
Copy the code

In Swift, structures and classes have a lot in common:

  • Define attributes to store values
  • Define methods to provide functionality
  • Defines subscript operations that access their values through subscript syntax
  • Define the constructor used to set initial values
  • Extend to add functionality beyond the default implementation
  • Follow protocols to provide some standard functionality

Classes have the following additional capabilities compared to constructs:

  • Inheritance: Allows a class to inherit the characteristics of another class
  • Type conversion: Allows the type of an instance of a class to be checked and interpreted at run time
  • Destructor: Allows an instance of a class to release any allocated resources
  • Reference counting: Allows multiple references to a class

Additional functionality supported by classes comes at the expense of complexity. Structures are generally preferred because they are easier to understand, and classes are used only when appropriate or necessary. In fact, most custom data types will be structures and enumerations.

The essential difference

Structs are value types and classes are reference types (pointer types)

  • Value types

All structures and enumerations in Swift are value types. When it is assigned to a variable, constant, or passed to a function, its contents are copied. Equivalent to “deep copy”, the two are independent of each other.

  • Reference types

When given to a variable, constant, or passed to a function, its value is not copied. Therefore, a reference to an existing instance is used, not a copy of it. Equivalent to shallow copy.

Because classes are reference types, multiple constants and variables may, behind the scenes, simultaneously refer to the same class instance. To determine whether two constants or variables refer to the same class instance, Swift provides two identity operators: same ===, different! = =

Because the structure is of value type. When an instance of a value type is declared as a constant, all of its attributes become constants. Classes belonging to reference types are different. After assigning an instance of a reference type to a constant, you can still modify the mutable properties of that instance.

Struct and class instances

To create a custom type or structure, you need to create an instance of it. Both structures and classes use the constructor syntax to create new instances. The simplest form of constructor syntax is to follow the type name of a structure or class with a pair of empty parentheses, as in:

let someResolution = Resolution()
let someVideoMode = VideoMode()
Copy the code

Any class or struct instance created in this way will have its properties initialized to default values.