Swift class

The Swift class is a common and flexible construct used to build code.

We can define properties (constants, variables) and methods for classes.

Unlike other programming languages, Swift does not require you to create separate interfaces and implementation files for custom classes. All you have to do is define a class in a single file, and the system automatically generates an external interface for other code.

Class and structure comparison

Classes and structures in Swift have a lot in common. Common to:

  • Define properties to store values
  • The definition method is used to provide functionality
  • Define the satellite script to use to access the value
  • The definition constructor is used to generate initialization values
  • Extend to increase the functionality of the default implementation
  • Conforms to the protocol to provide standard functionality for a certain class

Compared to structures, classes also have the following additional features:

  • Inheritance allows one class to inherit the characteristics of another class
  • Type conversion allows you to examine and interpret the type of a class instance at runtime
  • The deconstructor allows a class instance to release any resources it is allocated
  • Reference counts allow multiple references to a class

Grammar:

Class classname {
   Definition 1
   Definition 2
   ……
   Definition N
}

Class definition

class student{
   var studname: String
   var mark: Int 
   var mark2: Int 
}

Instantiation class:

let studrecord = student()

Instance

import Cocoa

class MarksStruct {
    var mark: Int
    init(mark: Int) {
        self.mark = mark
    }
}

class studentMarks {
    var mark = 300
}
let marks = studentMarks()
print("成绩为 \(marks.mark)")

The output of the above program execution is:

成绩为 300

Access class properties as a reference type

The properties of the class can . through . F ormat: Instant class name. Property name:

import Cocoa

class MarksStruct {
   var mark: Int
   init(mark: Int) {
      self.mark = mark
   }
}

class studentMarks {
   var mark1 = 300
   var mark2 = 400
   var mark3 = 900
}
let marks = studentMarks()
print("Mark1 is \(marks.mark1)")
print("Mark2 is \(marks.mark2)")
print("Mark3 is \(marks.mark3)")

The output of the above program execution is:

Mark1 is 300
Mark2 is 400
Mark3 is 900

Constant operator

Because a class is a reference type, it is possible to have multiple constants and variables in the background that reference a class instance at the same time.

To be able to determine whether two constants or variables reference the same class instance, Swift has two constant operators built in:

Constant operator Non-constant operator
The operator is: The operator is: !
True is returned if two constants or variables refer to the same class instance True is returned if two constants or variables refer to a different class instance

Instance

import Cocoa

class SampleClass: Equatable {
    let myProperty: String
    init(s: String) {
        myProperty = s
    }
}
func ==(lhs: SampleClass, rhs: SampleClass) -> Bool {
    return lhs.myProperty == rhs.myProperty
}

let spClass1 = SampleClass(s: "Hello")
let spClass2 = SampleClass(s: "Hello")

if spClass1 === spClass2 {// false
    print("引用相同的类实例 \(spClass1)")
}

if spClass1 !== spClass2 {// true
    print("引用不相同的类实例 \(spClass2)")
}

The output of the above program execution is:

引用不相同的类实例 SampleClass