先看下代码 👏
1
2
3
4
5
6
7
8
9
10
11
12
13
| protocol MyProtocol {
var var1: String { get set }
var var2: String { get }
}
struct MyStruct: MyProtocol {
var var1: String = ""
var var2: String = ""
}
let st = MyStruct(var1: "1", var2: "2")
st.var1 //1
st.var2 //2
|
当我们实例化 MyStruct
为 MyProtocol
类型 出现错误,说明:{ get set } & { set } 在protocol 级有效
1
2
3
4
| var testProtocol: MyProtocol = MyStruct()
testProtocol.var1 = "test var1" // No error
testProtocol.var2 = "test var2" // error: cannot assign to property: 'myVar2' is a get-only property
|
在声明协议的属性要求时,请始终使用{get},除非您非常确定要同时设置它和settable。
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
| protocol Flyable {
/// Limit the speed of flyable
var speedLimit: Int { get }
func fly()
}
extension Flyable {
func fly() {
print("Fly with speed limit: \(speedLimit)mph")
}
}
class Airplane: Flyable {
var speedLimit = 500
}
class 直升飞机: Flyable {
var speedLimit = 150
}
class SpeedLimitUpdater {
static func reduceSpeedLimit(of flyables: [Flyable], by value: Int) {
// Only update speedLimit of Helicopter & Airplane
for flyable in flyables {
if let helicopter = flyable as? 直升飞机 {
helicopter.speedLimit -= value
} else if let airplane = flyable as? Airplane {
airplane.speedLimit -= value
}
}
}
static func increaseSpeedLimit(of flyables: [Flyable], by value: Int) {
// Only update speedLimit of Helicopter & Airplane
for flyable in flyables {
if let helicopter = flyable as? 直升飞机 {
helicopter.speedLimit += value
} else if let airplane = flyable as? Airplane {
airplane.speedLimit += value
}
}
}
}
class Bird: Flyable {
private(set) var speedLimit = 20
}
// Bird 不是机器不能使用 SpeedLimitUpdater,Bird 的speedLimit限制private(set)
let bbb = Bird()
SpeedLimitUpdater.increaseSpeedLimit(of: [bbb], by: 1)
bbb.speedLimit //20
|
//当然我们可以内部更改
1
2
3
4
5
6
7
8
| class Bird: Flyable {
private(set) var speedLimit = 20
func changeSpeed() {
speedLimit += 10
}
}
bbb.changeSpeed()
bbb.speedLimit //30
|
get willset set didset
调用顺序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| class A {
var number :Int {
get {
print("get")
return 1
}
set {print("set")}
}
}
class B: A {
override var number: Int {
willSet {print("willSet")}
didSet {print("didSet")}
}
}
|
1
2
3
4
5
6
7
8
| let b = B()
b.number = 0
// 输出
// get
// willSet
// set
// didSet
|