You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A *deinitializer* is called immediately before a class instance is deallocated.
12
-
You write deinitializers with the `deinit` keyword,
13
-
similar to how initializers are written with the `init` keyword.
14
-
Deinitializers are only available on class types.
7
+
## 析构过程原理
15
8
16
-
## How Deinitialization Works
9
+
Swift 会在实例不再需要时自动释放它们,以释放资源。Swift 通过**自动引用计数**(*ARC*)来管理实例的内存,如 <doc:AutomaticReferenceCounting> 中所述。通常,在实例被释放时不需要执行手动清理。然而,当你处理自己的资源时,可能需要进行一些额外的清理。例如,如果创建一个自定义类来打开文件并向其中写入数据,可能需要在类实例被释放之前关闭文件。
17
10
18
-
Swift automatically deallocates your instances when they're no longer needed,
19
-
to free up resources.
20
-
Swift handles the memory management of instances through
21
-
*automatic reference counting* (*ARC*),
22
-
as described in <doc:AutomaticReferenceCounting>.
23
-
Typically you don't need to perform manual cleanup when your instances are deallocated.
24
-
However, when you are working with your own resources,
25
-
you might need to perform some additional cleanup yourself.
26
-
For example, if you create a custom class to open a file and write some data to it,
27
-
you might need to close the file before the class instance is deallocated.
28
-
29
-
Class definitions can have at most one deinitializer per class.
30
-
The deinitializer doesn't take any parameters
31
-
and is written without parentheses:
11
+
类定义中每个类最多只能有一个析构器。析构器不接受任何参数,并且是没有括号的:
32
12
33
13
```swift
34
14
deinit {
35
-
//perform the deinitialization
15
+
//执行析构过程
36
16
}
37
17
```
38
18
@@ -42,34 +22,19 @@ deinit {
42
22
```swifttest
43
23
>> class Test {
44
24
-> deinit {
45
-
// perform the deinitialization
25
+
// 执行析构过程
46
26
}
47
27
>> }
48
28
```
49
29
-->
50
30
51
-
Deinitializers are called automatically, just before instance deallocation takes place.
52
-
You aren't allowed to call a deinitializer yourself.
53
-
Superclass deinitializers are inherited by their subclasses,
54
-
and the superclass deinitializer is called automatically at the end of
55
-
a subclass deinitializer implementation.
56
-
Superclass deinitializers are always called,
57
-
even if a subclass doesn't provide its own deinitializer.
58
-
59
-
Because an instance isn't deallocated until after its deinitializer is called,
60
-
a deinitializer can access all properties of the instance it's called on
61
-
and can modify its behavior based on those properties
62
-
(such as looking up the name of a file that needs to be closed).
63
-
64
-
## Deinitializers in Action
65
-
66
-
Here's an example of a deinitializer in action.
67
-
This example defines two new types, `Bank` and `Player`, for a simple game.
68
-
The `Bank` class manages a made-up currency,
69
-
which can never have more than 10,000 coins in circulation.
70
-
There can only ever be one `Bank` in the game,
71
-
and so the `Bank` is implemented as a class with type properties and methods
> This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.
187
+
> 本文档包含有关正在开发的 API 或技术的初步信息。此信息可能会发生变化,根据本文档实施的软件应使用最终的操作系统一起进行测试。
254
188
>
255
-
> Learn more about using [Apple's beta software](https://developer.apple.com/support/beta-software/).
0 commit comments