类和结构体是人们构建代码所用的一种通用且灵活的构造体。我们可以使用完全相同的语法规则来为类和结构体定义属性(常量、变量)和添加方法,从而扩展类和结构体的功能。
与其他编程语言所不同的是,Swift 并不要求你为自定义类和结构去创建独立的接口和实现文件。你所要做的是在一个单一文件中定义一个类或者结构体,系统将会自动生成面向其它代码的外部接口。
-
类和结构的对比 (Comparing Structures and Classes)
Swift
中类和结构体有很多共同点:
定义属性用于存储值
定义方法用于提供功能
定义下标操作使得可以通过下标语法来访问实例所包含的值
定义构造器用于生成初始化值
通过扩展以增加默认实现的功能
实现协议以提供某种标准功能
类具有结构不具备的附加功能:
继承允许一个类继承另一个类的特征
类型转换允许在运行时检查和解释一个类实例的类型
析构器允许一个类实例释放任何其所被分配的资源
引用计数允许对一个类的多次引用
1.定义语法 (Definition Syntax)
struct SomeStructure {
// structure definition goes here
}
class SomeClass {
// class definition goes here
}
struct Resolution {
var width = 0
var height = 0
}
class VideoMode {
var resolution = Resolution()
var interlaced = false
var frameRate = 0.0
var name: String?
}
2.结构体和类实例 (Structure and Class Instances)
let someResolution = Resolution()
let someVideoMode = VideoMode()
3.访问属性 (Accessing Properties)
print("The width of someResolution is \(someResolution.width)")
// Prints "The width of someResolution is 0"
print("The width of someVideoMode is \(someVideoMode.resolution.width)")
// Prints "The width of someVideoMode is 0"
还可以使用点语法为变量属性分配新值:
someVideoMode.resolution.width = 1280
print("The width of someVideoMode is now \(someVideoMode.resolution.width)")
// Prints "The width of someVideoMode is now 1280"
4.结构体成员的初始化 (Memberwise Initializers for Structure Types)
let vga = Resolution(width: 640, height: 480)
-
结构体和枚举是值类型 (Structures and Enumerations Are Value Types)
值类型是一种类型,其值在被赋值给变量或常量时被复制,或者在传递给函数时被复制。
在前几章中,实际上已经广泛地使用了值类型。事实上,swift
中的所有基本类型——整数、浮点数、布尔值、字符串、数组和字典——都是值类型,并作为结构体在幕后实现。
所有结构体和枚举都是Swift
中的值类型。 这意味着您创建的任何结构和枚举实例以及它们作为属性的任何值类型在代码中传递时始终会被复制。
示例:
let hd = Resolution(width: 1920, height: 1080)
var cinema = hd
cinema.width = 2048
检查cinema
的width
属性,确实更改为2048
:
print("cinema is now \(cinema.width) pixels wide")
// Prints "cinema is now 2048 pixels wide"
但是,原hd
实例的width
属性仍然保持1920
的老值:
print("hd is still \(hd.width) pixels wide")
// Prints "hd is still 1920 pixels wide"
当cinema
被赋予当前的hd
值时,存储在hd
中的值被复制到新的cinema
实例中。最终结果是两个完全独立的实例,它们包含相同的数值。但是,由于它们是独立的实例,将cinema
的宽度设置为2048
并不影响hd
中存储的宽度,如下图所示:

同样的行为也适用于枚举:
enum CompassPoint {
case north, south, east, west
mutating func turnNorth() {
self = .north
}
}
var currentDirection = CompassPoint.west
let rememberedDirection = currentDirection
currentDirection.turnNorth()
print("The current direction is \(currentDirection)")
print("The remembered direction is \(rememberedDirection)")
// Prints "The current direction is north"
// Prints "The remembered direction is west"
-
类是引用类型 (Classes Are Reference Types)
与值类型不同,引用类型在分配给变量或常量或传递给函数时不会复制。使用对相同现有实例的引用,而不是副本。
下面这个例子,使用上面定义的VideoMode
类:
let tenEighty = VideoMode()
tenEighty.resolution = hd
tenEighty.interlaced = true
tenEighty.name = "1080i"
tenEighty.frameRate = 25.0
接下来,将tenEighty
分配给一个名为alsoTenEighty
的新常量,并修改alsoTenEighty
的帧速率:
let alsoTenEighty = tenEighty
alsoTenEighty.frameRate = 30.0
因为类是引用类型,所以tenEighty
和TenEighty
实际上都引用相同的VideoMode
实例。 实际上,它们只是同一个实例的两个不同名称,如下图所示:

检查
tenEighty
的frameRate
属性表明它正确地从底层VideoMode
实例报告新的帧速率30.0
:
print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
// Prints "The frameRate property of tenEighty is now 30.0"
标识符 (Identity Operators)
因为类是引用类型,所以多个常量和变量可能在后台引用同一个类的单个实例。 (对于结构和枚举,情况也是如此,因为它们在分配给常量或变量或传递给函数时总是被复制。)
有时可以找出两个常量或变量是否指向完全相同的类实例。 为了实现这一点,Swift
提供了两个标识符:
Identical to (===)
Not identical to (!==)
使用这些运算符来检查两个常量或变量是否引用同一个实例:
if tenEighty === alsoTenEighty {
print("tenEighty and alsoTenEighty refer to the same VideoMode instance.")
}
// Prints "tenEighty and alsoTenEighty refer to the same VideoMode instance."
网友评论