《浅入浅出Swift》Classes And Structures 类和结构体

####Classes And Structures

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129


//不会像c++那样用struct取代class关键字
//swift可以把class和结构体定义在一个.swift里,然后在其他文件里直接用就可以了
//不需要include,也不需要import

//1允许一个类继承另一个类的特点
//2允许在运行类实例时,检查和解释类型
//3使一个类的实例能够释放任何资源分配
//4引用计数允许多个引用一个类实例

//定义语法,definition syntax
//类和结构体定义首字母大写
class SomeClass{
var typeName = "class"
}
struct SomeStructure {
var typeName = "struct"
}
// var myFirstClass = SomeClass(typeName:"")//类不行
var myFirstClass = SomeClass()//定义一个变量,它是SomeClass类型的实例
println(myFirstClass.typeName)//访问这个实例的成员属性
//结构体有特别之处,在初始化的时候,可以把成员的值直接写在参数里面
var myFirstStr = SomeStructure(typeName:"my first struct")


struct Resolution{
var width = 0
var height = 0
}
class VideoMode {
var resolution = Resolution()//定义变量resolution,他是Resolution类型的实例
var interlaced = false
var frameRate = 0.0
var name : String = ""

}

//类和结构实例,Class and Structure Instances
let someResolution = Resolution()
let someVideoMode = VideoMode()
//访问属性,Accessing Properties
println("The width of someResolution is \(someResolution.width)")
println("The width of someVideoMode is \(someVideoMode.resolution.width)")
//点语法
someVideoMode.resolution.width = 1280
println("The width of someVideoMode is now \(someVideoMode.resolution.width)")

//为结构类型初始值设定项,Memberwise Initializers for Structure Types
let vga = Resolution(width: 650, height: 480)

//结构和枚举值类型,Structures and Enumerations Are Value Types
let hd = Resolution(width: 1920, height: 1080)
var cinema = hd
cinema.width = 2048
println("cinema.width now = \(cinema.width)")
//However, the width property of the original hd instance still has the old value of 1920:
println("hd.width now = \(hd.width)")

//同样的行为也适用于枚举,The same behavior applies to enumerations:
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")
}

//类是引用类型,Classes Are Reference Types
//这个例子声明一个新的常数称为tenEighty并集参考VideoMode类的一个新实例
//视频模式分配一份高清分辨为1920到1080之前,它是交错的,名称是“1080i”,最后它被设置成25.0帧每秒的帧速率
let tenEighty = VideoMode()
tenEighty.resolution = hd
tenEighty.interlaced = true
tenEighty.name = "1080i"
tenEighty.frameRate = 25.0

let alsoTenEighty = tenEighty
alsoTenEighty.frameRate = 30.0
println("now frameRate = \(alsoTenEighty.frameRate)")

//标识操作符,Identity Operators
//用来判断两个变量或常量,是否同时引用同一个类的实例,相同的 === 不相同的 !==
if tenEighty === alsoTenEighty{
println("tenEighty and alsoTenEighty refer to the same VideoMode instance")
}

//Array和Dictionar都是Struct
var myDictionary = [1:"1",2:"2",3:"3"]
var myDictionaryCopy = myDictionary;

myDictionary[1] = "8"
println(myDictionaryCopy[1])// = 1

var myArray = [1,2,3]
var myArrayCopy = myArray
myArray[1] = 8
println(myArray[1]) //得到结果是8
println(myArray === myArrayCopy)//false

//swift对Array做了优化,只有在必要时,才会分配一份拷贝
//Array只有在数组长度变化的时候才拷贝
//1 unshare() arr变 newArr不变 Xcode6 beta1,2
/*
var arr = [0,0,0]
var newArr = arr
arr.unshare()
arr[0] = 1
arr //[1, 0, 0]
newArr //[0, 0, 0]
*/

//2 copy() 直接拷贝 arr变,newArr不变 Xcode6 beta1,2
/*
var arr = [0,0,0]
var copiedArr = arr.copy()
arr[0] = 1
arr //[1, 0, 0]
copiedArr //[0, 0, 0]
*/


//值类型特点
var arr = [0,0,0]
// arr[0]=1//arr = [1,0,0], newArr =[1,0,0]
var newArr = arr//Xcode6 beta3 newArr = [0,0,0], beta1,2 newArr = [1,0,0]
arr[0]=1//arr = [1,0,0], newArr =[0,0,0]
println(arr)
println(newArr)

Reference

坚持原创技术分享,您的支持将鼓励我继续创作!
0%