Classes and Structures
类和结构体的比较
共同点:
- 定义存储值的属性
- 定义方法
- 定义使用脚本语法定义访问属性值的脚本(arr[i])
- 定义设置初始化状态的初始化器
- 可以使用extension关键字扩展功能
- 可以实现某种标准功能的协议(Protocols)
类有而结构体没有的功能:
- 一个类可以继承另一个类
- 在运行时,类型转换能让你检查和转换类的实例的类型
- 反初始化器能让类的实例释放分配的资源
- 引用计数允许一个类的实例有一个或多个引用(赋值或传递参数时,传递的是类实例的地址)
定义类和结构体的语法
定义结构体:
struct Resolution {
var width = 0
var height = 0
}
定义类:
class VideoMode {
var resolution = Resolution()
var interlaced = false
var frameRate = 0.0
var name: String?
}
类和结构体的实例化
let someResolution = Resolution()
let someVideoMode = VideoMode()
访问属性
获取属性:
print("The width of someResolution is \(someResolution.width)")
print("The width of someVideoMode is \(someVideoMode.resolution.width)")
对属性赋值:
someVideoMode.resolution.width = 1280
print("The width of someVideoMode is now \(someVideoMode.resolution.width)")
// Prints "The width of someVideoMode is now 1280"
对结构体类型的所有成员初始化
let vga = Resolution(width: 640, height: 480)
结构体和枚举是值类型的
赋值时或作为参数时都会拷贝一份:
let hd = Resolution(width: 1920, height: 1080)
var cinema = hd
cinema.width = 2048
print("cinema is now \(cinema.width) pixels wide")
// Prints "cinema is now 2048 pixels wide"
print("hd is still \(hd.width) pixels wide")
// Prints "hd is still 1920 pixels wide"
结构体hd赋值给cinema时,把hd拷贝了一份给cinema,所以cinema的属性值改变时,hd的属性不会改变.
枚举,在赋值时,或作为参数时也都会拷贝一份:
enum CompassPoint {
case north, south, east, west
}
var currentDirection = CompassPoint.west
let rememberedDirection = currentDirection
currentDirection = .east
if rememberedDirection == .west {
print("The remembered direction is still .west")
}
// Prints "The remembered direction is still .west"
类时引用类型
类在赋值时或作为参数时传递的是实例的地址:
let tenEighty = VideoMode()
tenEighty.frameRate = 25.0
let alsoTenEighty = tenEighty
alsoTenEighty.frameRate = 30.0
print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
// Prints "The frameRate property of tenEighty is now 30.0"
类在赋值时或作为参数时传递的是实例的地址,所以tenEighty赋值给alsoTenEighty时,alsoTenEighty和tenEighty指向相同的实例地址,因此修改alsoTenEighty的属性时,tenEighty的属性也会变。
同一性操作
两个变量或常量是否指向同一个类的实例的地址,Swift提供了两个同一性操作:
- ===,是指向同一个类的实例的地址
- !==,不是指向同一个类的实例的地址
if tenEighty === alsoTenEighty {
print("tenEighty and alsoTenEighty refer to the same VideoMode instance.")
}
// Prints "tenEighty and alsoTenEighty refer to the same VideoMode instance."
=== 和== 的区别
- === 判断两个变量或常量是否指向同一个类的实例的引用
- ==判断两个类的实例的值是否相等。
和Java不同,Swift的“==” 相当与Java中的equals方法,“==”也是Swift的方法。
指针
Swift中的常量或变量指向一个引用类型的实例类似与C中的指针。
类和结构体之间的选择
类和结构体都可以自定义自己的数据类型,但是结构体的实例传递的是值,类的实例传递的是引用。
因此可以在符合以下几种情况下使用机构体:
- 结构体主要是用来封装一写简单的数据。
- 在分配或传递该结构体的实例时,期望封装的值将被复制而不是引用。
- 结构体存储的任何属性本身都是值类型,它也将被复制,而不是引用。
- 结构体不需要从已有的类型中继承属性或行为。
Strings, Arrays, 和 Dictionaries的赋值和拷贝行为
在Swift中,String, Array, 和 Dictionary都是结构体。 因此当这些数据赋值给一个常量或变量,又或者作为一个方法或函数的参数传递时,都会被拷贝一份。