Swift Classes

Swift classes are general-purpose, flexible constructs used for building code.

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

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

Classes and Structures Comparison

In Swift, classes andstructuresshare many similarities. The commonalities are:

  • Define properties to store values
  • Define methods to provide functionality
  • Define subscripts to access values
  • Define initializers to generate initialization values
  • Use extensions to add default-implemented functionality
  • Conform to protocols to provide standard functionality for a class

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

  • Inheritance allows a class to inherit characteristics from another class
  • Type casting allows checking and interpreting the type of a class instance at runtime
  • Deinitializers allow a class instance to release any resources it has been allocated
  • Reference counting allows multiple references to a class instance

Syntax:

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

Class Definition

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

Instantiating a 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("成绩为 \(marks.mark)")

The output of the above program is:

成绩为 300

Accessing Class Properties as Reference Types

Class properties can be accessed through.to access. The format is:instantiatedClassName.propertyName:

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 is:

Mark1 is 300
Mark2 is 400
Mark3 is 900

Identity Operators

Because classes are reference types, it is possible for multiple constants and variables to reference the same class instance behind the scenes.

In order to determine whether two constants or variables refer to the same class instance, Swift has built-in two identity operators:

Identity operator Non-identity operator
The operator is: === The operator is: !==
Returns true if two constants or variables reference the same class instance Returns true if two constants or variables reference 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("引用相同的类实例 \(spClass1)")
}

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

The output of the above program is:

引用不相同的类实例 SampleClass
Other Extensions