Swift class


Release date:2023-12-01 Update date:2023-12-08 Editor:admin View counts:113

Label:

Swift class

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

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

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 will automatically generate external interfaces for other code.

Comparison of classes and structures

Classes and structures in Swift have a lot in common. What they have in common is:

  • Define properties to store values

  • Define methods to provide functionality

  • Define satellite scripts to access values

  • Define the constructor used to generate initialization values

  • Extend to add functionality implemented by default

  • Comply with the agreement to provide standard functionality for a class

Compared with structures, classes have the following additional functions:

  • Inheritance allows one class to inherit the characteristics of another class

  • Type conversion allows you to check and interpret the type of an instance ofa class at run time

  • The destructor allows a class instance to release any resources it is assigned

  • Reference counting allows multiple references to a class

Syntax:

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

Class definition

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

Instantiate the class:

let studrecord = student()

Example

import Cocoa

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

class studentMarks {
    var mark = 300
}
let marks = studentMarks()
print("The score is \(marks.mark)")

The output of the above program execution is as follows:

The score is 300

Access class properties as a reference type

The properties of the class can be accessed through the . to visit. Theformat is: instantiate the class name. Attribute 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 as follows:

Mark1 is 300
Mark2 is 400
Mark3 is 900

Identity operator

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

To determine whether two constants or variables refer to the same class instance, Swift has two identity operators built into it:

Identity operator

Unequal operator

The operator is: =

Operator is:! = =

Returns true if two constants or variables refer to the same class instance

Returns true if two constants or variables refer to different class instances

Example

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("Referencing the same class instance \(spClass1)")
}

if spClass1 !== spClass2 {// true
    print("Referencing different class instances \(spClass2)")
}

The output of the above program execution is as follows:

Referencing different class instances SampleClass

Powered by TorCMS (https://github.com/bukun/TorCMS).