结构体
- 在swift 标准库中,绝大多数的公开类型都是结构体,而枚举和类只占很小一部分
- 比如Bool、int、double、string、array、dictionary等常见类型都是结构体
struct Data {
var year: Int
var month: Int
var day: Int
}
//调用,可以传入所有的成员值,用以初始化所有成员(存储属性)
var data = Data(year: 2021, month: 2, day: 23)
- 所有结构体都有一个编译器生成的初始化器(initializer,初始化方法,构造器,构造方法)
自定义初始化器
- 一旦在定义结构体是自定义了初始化器,编译器就不会再自动生成其他的初始化器
struct Point {
var x: Int = 0
var y: Int = 0
init(x: Int, y: Int) {
self.x = x
self.y = y
}
}
var p1 = Point(x: 10, y: 10)
类
- 类的定义和结构体类似,但是编译器并没有为类自动生成可以传入成员值的初始化器
class Point {
var x: Int = 0
var y: Int = 0
}
let p1 = Point()
let p2 = Point(x: 10, y: 20) //报错
let p3 = Point(x: 10) //报错
类的初始化器
- 如果类的所有成员都在定义的时候指定了初始化值,编译器会为类生成无参的初始化器
- 成员的初始化是在这个初始化器中完成的
class Point {
var x: Int = 0
var y: Int = 0
}
let p1 = Point()
||
class Point {
var x: Int = 0
var y: Int = 0
init() {
x = 10
y = 20
}
}
let p1 = Point()
结构体和类的本质区别
- 结构体是值类型(枚举也是值类型),类是引用类型(指针类型)
class Size { struct Point {
//创建类的实例对象需要向堆空间申请内存
var width = 1 var x = 3
var height = 2 var y = 4
} }
值类型
- 值类型赋值给var、let或者给函数传值,是直接将所有内容拷贝一份
- 类似将文件进行copy、paste操作,产生了全新的文件副本。属于深拷贝
struct Point {
var x: Int
var y: Int
}
func test() {
var p1 = Point(x: 10, y: 20)
var p2 = p1
}
值类型的复制操作
//字符串
var s1 = "jack"
var s2 = s1
s2.append("_Rose")
print(s1)// jack
print(s2)// jack_Rose
//数组
var a1 = [1, 2, 3]
var a2 = a1
a1[0] = 2
a2.append(4)
print(a1)// [2, 2, 3]
print(a2) //[1, 2, 3, 4]
//字典
var d1 = ["max" : 10, "min" : 2]
var d2 = d1
d1["other"] = 7
d2["max"] = 12
print(d1)// ["max": 10, "min": 2, "other": 7]
print(d2)// ["max": 12, "min": 2]
引用类型
class Size {
var width: Int
var height: Int
init(width: Int, height: Int) {
self.width = width
self.height = height
}
}
func test() {
var s1 = Size(width: 10, height: 20)
var s2 = s1
}
引用类型的赋值操作
class Size {
var width: Int
var height: Int
init(width: Int, height: Int) {
self.width = width
self.height = height
}
}
var s1 = Size(width: 10, height: 20)
s1 = Size(width: 11, height: 22)
值类型、引用类型的let
struct Point {
var x: Int
var y: Int
}
let p = Point(x: 10, y: 20)
p = Point(x: 11, y: 22)// 报错
p.x = 33 // 报错
p.y = 44 // 报错
class Size {
var width: Int
var height: Int
init(width: Int, height: Int) {
self.width = width
self.height = height
}
}
let s = Size(width: 10, height: 20)
s = Size(width: 11, height: 22) // 报错
s.width = 33
s.height = 44
嵌套类型
struct Poker {
enum Suit : Character {
case spades = "♠", hearts = "♥", diamonds = "♦", clubs = "♣"
}
enum Rank: Int {
case two = 2, three, four, five, six, seven, eight, nine, ten
case jack, queen, king, ace
}
}
print(Poker.Suit.hearts.rawValue)
var suit = Poker.Suit.spades
suit = .diamonds
var rank = Poker.Rank.five
rank = .king
结构体、枚举、类都可以进行定义方法
// 结构体
struct Point {
var x = 10
var y = 10
func show() {
print("x = \(x), y = \(y)")
}
}
let p = Point()
p.show() // x = 10, y = 10
// 枚举
enum PokerFace: Character {
case spades = "♠", hearts = "♥", diamonds = "♦", clubs = "♣"
func show() {
print("face is \(rawValue)")
}
}
let pf = PokerFace.hearts
pf.show() // face is ♥
// 类
class Size {
var width = 10
var height = 10
func show() {
print("width = \(width), height = \(height)")
}
}
let s = Size()
s.show() //width = 10, height = 10
网友评论