@@ -70,8 +70,12 @@ and is used throughout this guide when referring to the type of an array.
70
70
71
71
### Creating an Empty Array
72
72
73
- You can create an empty array of a certain type
74
- using initializer syntax:
73
+ You can create an empty array in Swift using two approaches.
74
+ If the context already provides type information,
75
+ such as a function argument or an already typed variable or constant,
76
+ you can use an empty array literal,
77
+ which is written as ` [] `
78
+ (an empty pair of square brackets):
75
79
76
80
``` swift
77
81
var someInts: [Int ] = []
@@ -89,14 +93,24 @@ print("someInts is of type [Int] with \(someInts.count) items.")
89
93
```
90
94
-->
91
95
92
- Note that the type of the ` someInts ` variable is inferred to be ` [Int] `
93
- from the type of the initializer.
96
+ Alternatively, you can create an empty array of a certain type
97
+ using explicit initializer syntax,
98
+ by writing the element type in square brackets
99
+ followed by parentheses ---
100
+ for example, ` [Int]() ` in the following:
94
101
95
- Alternatively, if the context already provides type information,
96
- such as a function argument or an already typed variable or constant,
97
- you can create an empty array with an empty array literal,
98
- which is written as ` [] `
99
- (an empty pair of square brackets):
102
+ ``` swift
103
+ var someInts = [Int ]()
104
+ print (" someInts is of type [Int] with \( someInts.count ) items." )
105
+ // Prints "someInts is of type [Int] with 0 items."
106
+ ```
107
+
108
+ Both approaches produce the same result.
109
+ However,
110
+ an empty array literal is shorter and usually easier to read.
111
+
112
+ In both cases, you can use the empty array literal (` [] ` ) to
113
+ reassign an empty array to an existing variable:
100
114
101
115
``` swift
102
116
someInts.append (3 )
0 commit comments