1 You can initialize an array with an array literal, simply by putting one or more values together. The array literal is written as a comma-separated line of values, and the lines are wrapped up in parentheses with one end of each other:2[Value1, value2, value3]3 4 The following example creates an array that holds a string of type value named Shoppinglist:5var shoppinglist:string[] = ["Eggs","Milk"]6 //shoppinglist initialization with two elements7According to string[], the variable shoppinglist is declared as "an array that holds string values." Because this array indicates that the value type is string, it can only hold a value of type string. The shoppinglist array here is initialized with an array literal that contains two elements of type string ("Eggs"And"Milk"). 8 Note: The array shoppinglist is declared as a variable (with the keyword Var) instead of a constant (with the keyword let) because more elements are added to the shopping list in the following example. 9 in this case, the array literal contains only two string values. This is consistent with the type in the shoppinglist variable definition (an array that holds string values), so you can assign the array literal directly to the shoppinglist for initialization. Ten One thanks to Swift's type inference, if you use array literals to initialize an array, the values in the array literal have the same type, and you don't have to explicitly write out the type of the array. The code for initializing shoppinglist above can be abbreviated as: Avar shoppinglist = ["Eggs","Milk"] -Because all values in the array literal have the same type, swift can infer that the type of the shoppinglist variable is string[].
Swift array literal