English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Swift classes are a general 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 interface and implementation files for custom classes. What you need to do is define a class in a single file, and the system will automatically generate an external interface for other code.
Swift andStructuresThere are many similarities. The similarities are in:
Define properties to store values
Define methods to provide functionality
Define computed properties to access values
Define constructors to generate initial values
Extend to add default implementation functionality
Conform to protocols to provide standard functionality for a class
Compared to structures, classes have the following additional features:
Inheritance allows a class to inherit the characteristics of another class
Type casting allows the type of a class instance to be checked and interpreted at runtime
A deinitializer allows an instance of a class to release any resources it has allocated
Reference counting allows multiple references to a class
class classname { Definition 1 Definition 2 …… Definition N }
class student{ var studname: String var mark: Int var mark2: Int }
Example class:
let studrecord = student()
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 is as follows:
The score is 300
Class properties can be accessed through . to access. The format is:Example 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 is as follows:
Mark1 is 300 Mark2 is 400 Mark3 is 900
Because classes are reference types, there may be multiple constants and variables in the background that simultaneously refer to a particular instance of a class.
To determine whether two constants or variables refer to the same instance of a class, Swift has built-in two identity operators:
Identity operator | Non-equality operator |
---|---|
The operator is: === | The operator is: !== |
Returns true if two constants or variables refer to the same instance of a class | Returns true if two constants or variables refer to different instances of a class |
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("Example of classes with the same reference \(spClass1)") } if spClass1 !== spClass2 {// true print("Example of classes with different references \(spClass2)") }
The output of the above program is as follows:
Example of classes with different references SampleClass