类和结构体
Swfit并不要求你为自定义类和结构去创建独立的接口和实现文件,只需要在一个文件定义一个类或结构体,Swift自动生成外部接口
通常类的实例被称为对象,Swift中称为实例而不是对象
枚举也是一样的
以下tenEighty和alsoTenEighty虽然是常量,但依然可以修改它们的属性,因为这两个常量本身不会被改变,仅仅存了ViewMode()实例的引用
等价于( === ) 表示两个类类型的常量或变量引用同一个实例
不等价( !== )
值传递
主要用来封装少量相关简单的数据值
构建一个实例在赋值或传递时,要明确封装的数据是被拷贝而不是引用
在结构体中存储的值类型属性,都是被拷贝而不是引用
结构体不需去继承另一个已存在在类型的属性或行为
在实际应用中,绝大部分的自定义数据都应该是类,而非结构体
字典类型的赋值和拷贝
如果字典中存储的key/value是值类型,在赋值或调用时,都会被拷贝,如果是引用类型,将会被引用
数组的赋值和拷贝
数组的赋值和拷贝要比字典复杂,当操作数组时能提供接近C语言的功能,并且拷贝行为只在必要时才发生,将数组赋值给其他变量时,数组的内容不会被拷贝,而是被共享出来公用一个,当数组元素被修改,在另一组数组也显示同样的结果,而拷贝行为只在修改了数组长度的时候发生,发生拷贝的情况与字典类似
或者判断两个数组是否共用相同的元素
unshare方法仅会在确认有必要的时候菜户创建数组拷贝,copy方法在任何时候都会创建新的拷贝,即使引用已经是唯一的
Swfit并不要求你为自定义类和结构去创建独立的接口和实现文件,只需要在一个文件定义一个类或结构体,Swift自动生成外部接口
通常类的实例被称为对象,Swift中称为实例而不是对象
1.定义
通过class和struct来声明结构体,大括号里定义它们的内容- class SomeClass {
- // class definition goes here
- }
- struct SomeStructure {
- // structure 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.类和结构体的实例
使用初始化方法来生成新的实例- let someResolution = Resolution()
- let someVideoMode = VideoMode()
3.访问属性
使用点语法访问类和结构体的属性,或者子属性- println("The width of someResolution is \(someResolution.width)")
- // prints "The width of someResolution is 0"
- println("The width of someVideoMode is \(someVideoMode.resolution.width)")
- // prints "The width of someVideoMode is 0"
- someVideoMode.resolution.width = 1280
- println("The width of someVideoMode is now \(someVideoMode.resolution.width)")
- // prints "The width of someVideoMode is now 1280"
4.结构体类型的初始化方法
所有结构体都有一个自动生成的初始化方法,用于初始化结构体中成员的属性- let vga = Resolution(width: 640, height: 480)
5.结构体和枚举都是值类型的
值类型在赋值给变量、常量或者传入到一个函数的时候,操作的都是值的拷贝,意味着它们的实例以及实例所包含的属性在代码传值的时候都会被复制- //以下定义一个hd常量,值为(1920,1080)像素的Resolution实例,然后定义cinema变量,其值为hd,cinema的值其实是hd得拷贝副本而不是hd本身,它们是两个不同的实例
- let hd = Resolution(width: 1920, height: 1080)
- var cinema = hd
- //将cinema的width属性改变,而hd实例的中不会改变,两者的修改并不会相互影响对方,因为hd赋值给cinema的时候,实际是将hd存储的值进行拷贝,然后将拷贝的值存入cinema中
- cinema.width=2048
- println("cinema is now \(cinema.width) pixels wide")
- // prints "cinema is now 2048 pixels wide"
- println("hd is still \(hd.width) pixels wide")
- // prints "hd is still 1920 pixels wide"
- enum CompassPoint {
- case North, South, East, West
- }
- var currentDirection = CompassPoint.West
- let rememberedDirection = currentDirection
- currentDirection = .East
- if rememberedDirection == .West {
- println("The remembered direction is still .West")
- }
- // prints "The remembered direction is still .West"
6.类是引用类型
与值类型不同,引用类型赋予到一个变量或常量之后,操作的不是拷贝,而是实例本身以下tenEighty和alsoTenEighty虽然是常量,但依然可以修改它们的属性,因为这两个常量本身不会被改变,仅仅存了ViewMode()实例的引用
- let tenEighty = VideoMode()
- tenEighty.resolution = hd
- tenEighty.interlaced = true
- tenEighty.name = "1080i"
- tenEighty.frameRate = 25.0
- let alsoTenEighty = tenEighty
- alsoTenEighty.frameRate = 30.0
- println("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
- // prints "The frameRate property of tenEighty is now 30.0"
7.恒等运算符
因为类是引用类型,可能出现多个变量在引用同一个类实例的情况,所以Swift提供了两个恒等运算符,判断两个常量或者变量是否引用同一个类实例等价于( === ) 表示两个类类型的常量或变量引用同一个实例
不等价( !== )
- if tenEighty === alsoTenEighty {
- println("tenEighty and alsoTenEighty refer to the same Resolution instance.")
- }
- // prints "tenEighty and alsoTenEighty refer to the same Resolution instance."
8.类和结构体的选择
结构体:值传递
主要用来封装少量相关简单的数据值
构建一个实例在赋值或传递时,要明确封装的数据是被拷贝而不是引用
在结构体中存储的值类型属性,都是被拷贝而不是引用
结构体不需去继承另一个已存在在类型的属性或行为
在实际应用中,绝大部分的自定义数据都应该是类,而非结构体
9.集合类型的赋值和拷贝行为
Swift中的数组和字典都是结构体,当数组被赋值给一个常量或变量,或传递到函数或方法时,和字典的拷贝都与其他结构体有不同字典类型的赋值和拷贝
如果字典中存储的key/value是值类型,在赋值或调用时,都会被拷贝,如果是引用类型,将会被引用
- //以下字典key/value 分别是String/Int,都是值类型,字典被拷贝时,两者都被拷贝
- var ages = ["Peter": 23, "Wei": 35, "Anish": 65, "Katya": 19]
- var copiedAges = ages
- copiedAges["Peter"] = 24
- println(ages["Peter"])
- // prints "23"
数组的赋值和拷贝
数组的赋值和拷贝要比字典复杂,当操作数组时能提供接近C语言的功能,并且拷贝行为只在必要时才发生,将数组赋值给其他变量时,数组的内容不会被拷贝,而是被共享出来公用一个,当数组元素被修改,在另一组数组也显示同样的结果,而拷贝行为只在修改了数组长度的时候发生,发生拷贝的情况与字典类似
- //将整数(Int)数组赋值给a,b,c变量
- var a = [1, 2, 3]
- var b = a
- var c = a
- println(a[0])
- // 1
- println(b[0])
- // 1
- println(c[0])
- // 1
- a[0] = 42
- println(a[0])
- // 42
- println(b[0])
- // 42
- println(c[0])
- // 42
- //当长度发生变化,Swift会创建这个数组的拷贝,a将会是原数组的一个独立拷贝
- a.append(4)
- a[0] = 777
- println(a[0])
- // 777
- println(b[0])
- // 42
- println(c[0])
- // 42
10.确保数组的唯一性
操作数组的时候有必要确认这个数组是有一个唯一拷贝的,调用unshare方法确定数组引用的唯一性,如果一个数组被多个变量引用,在其中的一个变量调用unshare方法则会拷贝此数组,此时这个变量会有属于他自己的独立数组拷贝。- b.unshare()
- b[0] = -105
- println(a[0])
- // 777
- println(b[0])
- // -105
- println(c[0])
- // 42
11.判断两个数组是否共用相同元素
- if b === c {
- println("b and c still share the same array elements.")
- } else {
- println("b and c now refer to two independent sets of array elements.")
- }
- // prints "b and c now refer to two independent sets of array elements."
- if b[0...1] === b[0...1] {
- println("These two subarrays share the same elements.")
- } else {
- println("These two subarrays do not share the same elements.")
- }
- // prints "These two subarrays share the same elements."
12.强制复制数组
通过调用copy方法进行强制复制,这方法对数组进行浅拷贝,并返回一个包含此拷贝的新数组,- var names = ["Mohsen", "Hilary", "Justyn", "Amy", "Rich", "Graham", "Vic"]
- var copiedNames = names.copy()
- copiedNames[0] = "Mo"
- println(names[0])
- // prints "Mohsen"